Check if a string can be formed from another string by at most X circular clockwise shifts | C ++ implementation to check that a given string can be converted to another string by circular clockwise shift of each character by atmost X times ; Function to check that all characters of s1 can be converted to s2 by circular clockwise shift atmost X times ; Check for all characters of the strings whether the difference between their ascii values is less than X or not ; If both the characters are same ; Calculate the difference between the ASCII values of the characters ; If difference exceeds X ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void isConversionPossible ( string s1 , string s2 , int x ) { int diff , n ; n = s1 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) continue ; diff = ( int ( s2 [ i ] - s1 [ i ] ) + 26 ) % 26 ; if ( diff > x ) { cout << " NO " << endl ; return ; } } cout << " YES " << endl ; } int main ( ) { string s1 = " you " ; string s2 = " ara " ; int x = 6 ; isConversionPossible ( s1 , s2 , x ) ; return 0 ; }