JavaScript Program to Check if a string can be formed from another string by at most X circular clockwise shifts


Circular clockwise shifts for the string mean rotating the characters of the string to their next character in alphabetic order. For each circular shift characters are converted to the next character and if there is no character present after the certain rotations (as ‘z’ is the last character of the English alphabet), then for that case we can assume characters in the cycle, and again start from the first character of English alphabets.

We will be given two strings and we can rotate each character of the first string maximum of x numbers of times, and after doing a certain number of required and maximum changes will find out whether both the strings are equal or not. If both the strings are equal within the maximum X shifts of each iteration, then we will print yes otherwise no.

Example

Example 1:

For example, we are given the string1: “abcdef”, the second string is: “cccdfi”, and the maximum number of rotations possible is three.

Output: Yes

Explanation: For the first character ‘a’ we will rotate it 2 times and we will get ‘c’

For the second character ‘b’ we will rotate it 1 time and we will get ‘c’

There is no need to rotate ‘c’ and ‘d’.

For the characters ‘e’ and ‘f’ we need 1 and 3 rotations respectively.

So, we can make string2 from string1 in a maximum of 3 rotations.

Example2:

We are given string1 ‘abcdef’, the second string is: ‘daddgh’, and the maximum number of rotations possible is two.

Output: No

Explanation: For the first character we need three rotations to convert ‘a’ to ‘d’ which is more than the maximum allowed.

For the second character, we need 25 rotations to convert ‘b’ to ‘a’ which is more than the maximum allowed.

Similarly, not possible for ‘e’, and ‘f’.

Approach

From the example, we can get the idea that we just have to traverse over the string once and check for each character. Let us see the steps:

  • First, we will get the length of both the strings and compare them, if they are not equal then we will not be able to convert the first string to another.

  • We will traverse over the string and for each character, we will check two things.

  • If the second string’s character is smaller as compared to the first string or the difference between both the characters is greater as compared to the given number.

  • If the above-mentioned cases are true, then we are not able to reach there and return false.

  • Else we will return true and print the result according to it.

Example

// defining the function to check if the given string can
// be converted to another or not
function check(string1, string2, number){

   // getting the length of the string1
   var len1 = string1.length
   var len2 = string2.length
   
   // if length of both the given strings is not equal then its impossible
   if(len1 != len2){
      return false;
   }
   
   // traversing over the array
   for(var i = 0; i<len1; i++) {
      if(string1[i] > string2[i] || (string2[i]-string1[i] > number)){
         return false;
      }
   }
   return true;
}

// defining the string
var string1 = "abcdef";
var string2 = "cccdfi";
var number = 3;
if(check(string1,string2,number)){
   console.log("Yes, we can convert string '" + string1 + "' to string '" + string2 + "' in given " + number + " number of rotations ");
}
else{
   console.log("No, we cannot convert string '" + string1 + "' to string '" + string2 + "' in given " + number + " number of rotations ");
}
string2 = "daddgh"
number = 2;
if(check(string1,string2,number)){
   console.log("Yes, we can convert string '" + string1 + "' to string '" + string2 + "' in given " + number + " number of rotations ");
}
else{
   console.log("No, we cannot convert string '" + string1 + "' to string '" + string2 + "' in given " + number + " number of rotations ");
}

Output

Yes, we can convert string 'abcdef' to string 'cccdfi' in given 3 number of rotations 
No, we cannot convert string 'abcdef' to string 'daddgh' in given 2 number of rotations 

Time and Space Complexity

The time complexity of the above code is O(N), where N is the number of characters present in the string. We are traversing over the strings only once makes the time complexity of the program linear.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a JavaScript program for two given strings and a number which indicates how many times we can rotate a single character to string to its clockwise direction and the task was to find whether both the strings are equal or not. The time complexity of the above code is O(N) which is linear and the space complexity of the above code is O(1).

Updated on: 13-Apr-2023

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements