Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times.
Input: S1 = “abcd”, S2 = “dddd”, X = 3
Output: Yes
Explanation:
Given string S1 can be converted to string S2 as-
Character “a” – Shift 3 times – “d”
Character “b” – Shift 2 times – “d”
Character “c” – Shift 1 times – “d”
Character “d” – Shift 0 times – “d”
Input: S1 = “you”, S2 = “ara”, X = 6
Output: Yes
Explanation:
Given string S1 can be converted to string S2 as –
Character “y” – Circular Shift 2 times – “a”
Character “o” – Shift 3 times – “r”
Character “u” – Circular Shift 6 times – “a”
Approach: The idea is to traverse the string and for each index and find the difference between the ASCII values of the character at the respective indices of the two strings. If the difference is less than 0, then for a circular shift, add 26 to get the actual difference. If for any index, the difference exceeds X, then S2 can’t be formed from S1, otherwise possible.
Below is the implementation of the above approach:
C++
// 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 #include <bits/stdc++.h> using namespace std; // Function to check that all // characters of s1 can be // converted to s2 by circular // clockwise shift atmost X times void isConversionPossible(string s1, string s2, int x) { int diff, n; n = s1.length(); // Check for all characters of // the strings whether the // difference between their // ascii values is less than // X or not for (int i = 0; i < n; i++) { // If both the characters // are same if (s1[i] == s2[i]) continue; // Calculate the difference // between the ASCII values // of the characters diff = (int(s2[i] - s1[i]) + 26) % 26; // If difference exceeds X if (diff > x) { cout << "NO" << endl; return; } } cout << "YES" << endl; } // Driver Code int main() { string s1 = "you"; string s2 = "ara"; int x = 6; // Function call isConversionPossible(s1, s2, x); return 0; } |
Java
// Java implementation to check // that a given string can be // converted to another string // by circular clockwise shift // of each character by atmost // X times import java.io.*; import java.util.*; class GFG{ // Function to check that all // characters of s1 can be // converted to s2 by circular // clockwise shift atmost X times static void isConversionPossible(String s1, String s2, int x) { int diff = 0, n; n = s1.length(); // Check for all characters of // the strings whether the // difference between their // ascii values is less than // X or not for(int i = 0; i < n; i++) { // If both the characters // are same if (s1.charAt(i) == s2.charAt(i)) continue; // Calculate the difference // between the ASCII values // of the characters diff = ((int)(s2.charAt(i) - s1.charAt(i)) + 26) % 26; // If difference exceeds X if (diff > x) { System.out.println("NO"); return; } } System.out.println("YES"); } // Driver Code public static void main (String[] args) { String s1 = "you"; String s2 = "ara"; int x = 6; // Function call isConversionPossible(s1, s2, x); } } // This code is contributed by Ganeshchowdharysadanala |
Python3
# Python3 implementation to check # that the given string can be # converted to another string # by circular clockwise shift # Function to check that the # string s1 can be converted # to s2 by clockwise circular # shift of all characters of # str1 atmost X times def isConversionPossible(s1, s2, x): n = len(s1) s1 = list(s1) s2 = list(s2) for i in range(n): # Difference between the # ASCII numbers of characters diff = ord(s2[i]) - ord(s1[i]) # If both characters # are the same if diff == 0: continue # Condition to check if the # difference less than 0 then # find the circular shift by # adding 26 to it if diff < 0: diff = diff + 26 # If difference between # their ASCII values # exceeds X if diff > x: return False return True # Driver Code if __name__ == "__main__": s1 = "you" s2 = "ara" x = 6 # Function Call result = isConversionPossible(s1, s2, x) if result: print("YES") else: print("NO") |
C#
// 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 using System; class GFG{ // Function to check that all // characters of s1 can be // converted to s2 by circular // clockwise shift atmost X times static void isConversionPossible(String s1, String s2, int x) { int diff = 0, n; n = s1.Length; // Check for all characters of // the strings whether the // difference between their // ascii values is less than // X or not for(int i = 0; i < n; i++) { // If both the characters // are same if (s1[i] == s2[i]) continue; // Calculate the difference // between the ASCII values // of the characters diff = ((int)(s2[i] - s1[i]) + 26) % 26; // If difference exceeds X if (diff > x) { Console.Write("NO"); return; } } Console.Write("YES"); } // Driver Code public static void Main () { String s1 = "you"; String s2 = "ara"; int x = 6; // Function call isConversionPossible(s1, s2, x); } } // This code is contributed by chitranayal |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Count of strings that can be formed from another string using each character at-most once
- Number of Counterclockwise shifts to make a string palindrome
- Sort a string lexicographically using triple cyclic shifts
- Count number of ways to convert string S to T by performing K cyclic shifts
- Minimum shifts of substrings of 1s required to group all 1s together in a given Binary string
- Check whether second string can be formed from characters of first string
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be repeated to make another string
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can be obtained by rotating another string d places
- Check if a string can be obtained by appending subsequences of another string
- Clockwise rotation of Linked List
- Rotate a matrix by 90 degree in clockwise direction without using any extra space
- Check if the given string of words can be formed from words present in the dictionary
- Check if given string can be formed by two other strings or their permutations
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check if K palindromic strings can be formed from a given string
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Check if a given string can be formed using characters of adjacent cells of a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

