Java Program for Check if a string can be formed from another string by at most X circular clockwise shifts Last Updated : 25 Jan, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 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 Output: YES Time Complexity:O(N),N=Length(S1) Auxiliary Space:O(1) Please refer complete article on Check if a string can be formed from another string by at most X circular clockwise shifts for more details! Comment More infoAdvertise with us Next Article Javascript Program to Check if a string can be obtained by rotating another string 2 places K kartik Follow Improve Article Tags : Java rotation ASCII Practice Tags : Java Similar Reads Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts 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 3 min read Javascript Program to Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str 4 min read Javascript Program to Check if a string can be obtained by rotating another string 2 places Given two strings, the task is to find if a string can be obtained by rotating another string in two places. Examples: Input: string1 = "amazon", string2 = "azonam" Output: Yes // rotated anti-clockwiseInput: string1 = "amazon", string2 = "onamaz" Output: Yes // rotated clockwiseAsked in: Amazon Int 2 min read Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str 9 min read Check if a string can be obtained by rotating another string 2 places Given two strings, str1 and str2, the task is to determine if str2 can be obtained by rotating str1 exactly 2 places in either a clockwise or anticlockwise direction.Examples: Input: str1 = "amazon", str2 = "azonam" Output: Yes Explanation: Rotating string1 by 2 places in anti-clockwise gives the st 11 min read Javascript Program to Check if all array elements can be converted to pronic numbers by rotating digits Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times. Examples: Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to 3 min read Like