Check String Formation by Circular Clockwise Shifts in JavaScript



To check if a string can be formed from another string by at most X circular clockwise shifts, we can calculate the difference of ASCII value of the characters and then compare it with the X. Circular clockwise shifts for the string mean rotating the characters of the string to their next character in alphabetic order.

In this article, we are given with a string, target and a number which represent the number of circular shifts to be made. Our task is to write a JavaScript program to check if the target can be formed from the string by at most X circular clockwise shifts.

Examples

Input:
string = "abcdez";
target = "cccdfb";
number = 3;

a+2 = c, b+1 = c
c+1 = d, d+0 = d
e+1 = f, z+2 = b

Output: true
Input:
string = "abc";
target = "dhe";
number = 3;

a+3 = d
b+6 = h
c+2 = e

Output: false

Steps to Check if a string can be formed from another string

We will be following below mentioned steps to check if the target can be formed from string with given number of circular shifts.

  • We have declared three variables, original string, target string and the number of clockwise shifts to be made. We have also defined a function check() which accepts these three variables as arguments.
  • We get the length of both the strings using length property and check if they are same using if/else statement. If length of both the strings are not equal then return false. The given string can not be converted into target.
  • If the length of the both strings are same, then we calculate the difference of ASCII code of each character of string and target by traversing each character of string using for loop.
  • The 26 is added to handle the wrapping around in circular alphabet such as from 'z' to 'a'. The modulus operation with 26 keeps the range of difference from 0-25.
  • Then we compare this difference with the given number. If the difference is greater than the number then target cannot be formed from string by given number of shifts and return false.
  • After the completion of loop true is returned, meaning target can be formed from string by given number of shifts.

Example

Here is an example code implementing above mentioned steps to check if target can be formed from the string with given number of circular clockwise shifts.

let string = "abcdez";
let target = "cccdfb";
let number = 3;

function check(string, target, number) {
    let len1 = string.length;
    let len2 = target.length;
    if (len1 !== len2) 
        return false;
    for (let i = 0; i  number) {
            return false;
        }
    }
    return true;
}

if (check(string, target, number)) {
    console.log(`Yes, we can convert string '${string}' to string '${target}' with at most ${number} circular shifts.`);
} else {
    console.log(`No, we cannot convert string '${string}' to string '${target}' with at most ${number} circular shifts.`);
}

Conclusion

In this article, we have discussed the steps to check if a string can be formed from another string by at most X circular clockwise shifts. The time complexity for the mentioned approach is O(n) and space complexity is O(1).

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2025-01-10T14:01:17+05:30

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements