- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for Check if a string can be formed from another string by at most X circular clockwise shifts
In this problem, we need to convert one string to another by performing at most x circular shift operations on each character of the first string.
The naïve approach to solving the problem is to rotate each character of the alpha1 string for x times, and we check if it matches the character of the alpha2 string, which is at the same index.
The second approach is to solve the problem by finding the circular difference between characters at the same index.
Problem statement – We have given a positive integer X. Also, we have given a string alpha1 and alpha2. The task is that we need to check whether we can convert the string alpha1 to alpha2 by performing at most X circular shifts on each character of alpha1. If it is possible to convert alpha1 to alpha2, print ‘Yes’. Else, print ‘No’.
Sample examples
Input
alpha1 = "abc"; alpha2 = "def", x = 3
Output
‘Yes’
Explanation – We can rotate each character of alpha1 at most 3 times. So, ‘a’ + 3 == ‘d’, ‘b’ + 3 == ‘e’, and ‘c’ + 3 == ‘f’.
Input
alpha1 = "abc"; alpha2 = "dnf", x = 3
Output
‘No’
Explanation – It is not possible to get ‘n’ from ‘b’ by performing at most 3 circular shifts.
Input
alpha1 = "stu"; alpha2 = "sxy", x = 9
Output
‘Yes’
Explanation – We can perform at most 9 circular shifts to update any character. So, for ‘s’ to ‘s’, we need 0 circular shifts, for ‘t’ to ‘x’, and ‘u’ to ‘y’, we need 4 circular shifts, which is smaller than 9. So, it is possible to convert alpha1 to alpha2.
Approach 1
In this approach, we will rotate each character of the alpha1 from 1 to x times. If we find a match with the character of the alpha2 string, we will move to the next iteration. Otherwise, we can say that it is not possible to convert alpha1 to alpha2.
Algorithm
Step 1 – Start traversing the alpha1 string.
Step 2 – If characters at the pth index in the alpha1 and alpha2 strings are the same, move to the next iteration. Otherwise, follow the below steps.
Step 3 – Define the ‘isCharSame’ boolean variable and initialize with false to keep track of whether the character of alpha2 matches with the character of alpha1 after rotating it by q.
Step 4 – Use another nested loop to make x iterations. In the nested loop, store the ASCII values of alpha1[p] and alpha2[p] into char1 and char2 after taking its modulo with 26.
Step 5 – Add ‘q’ to the ‘char1’ and take its modulo with 26 to circularly rotate the ‘char1’ by ‘q’.
Step 6 – If the rotated char value matches with the ‘char2’, change the ‘isCharSame’ variable to true, and break the loop.
Step 7 – Outside the nested loop, if the value of the ‘isCharSame’ variable is false, it is not possible to convert alpha1 to alpha2.
Step 8 – At last, if all characters of alpha1 match with alpha2 after making at most x circular shift, print ‘Yes’.
Example
import java.io.*; import java.util.*; public class Main { public static void CheckForConversion(String alpha1, String alpha2, int x) { // variables to store character difference and string length int str_len; str_len = alpha1.length(); // Traverse both strings for (int p = 0; p < str_len; p++) { // If a character is not the same at the pth index in both strings if (alpha1.charAt(p) != alpha2.charAt(p)) { // variable to track if the character is the same after rotation boolean isCharSame = false; // Make X iterations for (int q = 1; q <= x; q++) { // Get a character from the pth index and find its ASCII value int char1 = alpha1.charAt(p) % 26; int char2 = alpha2.charAt(p) % 26; // get character value between 1 to 26 after rotating by q index int char1Result = (char1 + q) % 26; // If chars are the same after adding q, break the loop if (char1Result == char2) { isCharSame = true; break; } } if (isCharSame == false) { System.out.println("It is not possible to convert string alpha1 to alpha2 according to given conditions."); return; } } } System.out.println("It is possible to convert string alpha1 to alpha2 according to given conditions."); } public static void main(String[] args) { String alpha1 = "abc"; String alpha2 = "def"; int x = 3; CheckForConversion(alpha1, alpha2, x); } }
Output
It is possible to convert string alpha1 to alpha2 according to given conditions.
Time complexity – O(N*X), as we traverse the string and rotate each character of the string at most X times.
Space complexity – O(1) as we don’t use dynamic space.
Approach 2
In this approach, we will take the character difference of both strings. If the character difference is greater than X, converting the alpha1 string to alpha2 is not possible.
Algorithm
Step 1 – Initialize the ‘char_diff’ and ‘str_len’ variables.
Step 2 – Start iterating the string. If characters at the pth index are the same in both strings, use the ‘continue’ keyword to move to the next character of the string.
Step 3 – Access characters from the pth index of both strings and subtract the ASCII value of alpha1[p] from alpha2[p]. Also, add 26 to the subtraction result and store it in the ‘char_diff’ variable.
Step 4 – Take a modulo of ‘char_diff’ with 26.
Step 5 – Print' No' if ‘char_diff’ is greter than X for any character. Also, execute the ‘return’ statement to terminate the function.
Step 6 – At last, print ‘Yes’.
Example
import java.io.*; import java.util.*; public class Main { public static void CheckForConversion(String alpha1, String alpha2, int x) { // variables to store character difference and string length int char_diff = 0, str_len; str_len = alpha1.length(); // Traverse both strings for (int p = 0; p < str_len; p++) { // If the character is the same at the pth index in both strings, move to the next iteration if (alpha1.charAt(p) == alpha2.charAt(p)) continue; // Difference calculation char_diff = ((int) (alpha2.charAt(p) - alpha1.charAt(p)) + 26); // Performing modulus operation for rotational difference char_diff = char_diff % 26; // If value of char_diff is more than x, it is not possible to convert string // alpha1 to alpha2 if (char_diff > x) { System.out.println("It is not possible to convert string alpha1 to alpha2 according to given conditions."); return; } } System.out.println("It is possible to convert string alpha1 to alpha2 according to given conditions."); } public static void main(String[] args) { String alpha1 = "abc"; String alpha2 = "def"; int x = 3; CheckForConversion(alpha1, alpha2, x); } }
Output
It is possible to convert string alpha1 to alpha2 according to given conditions.
Time complexity – O(N) for traversing the string to check character difference for each character.
Space complexity – O(1)
The first solution finds X circular rotations of the characters and matches it with the character of another string, but the second solution is more logical as it uses the character difference value to find the answer.