Rotate String Leetcode Question Solve - Javascript DSA Question

Sk ShoyebSk Shoyeb
1 min read

Now I will explain the solve of Rotate sting leetcode easy problem solve in javascript. The question is Given two strings s and goal, return trueif and only ifscan becomegoalafter some number ofshiftsons.

A shift on s consists of moving the leftmost character of s to the rightmost position.

  • For example, if s = "abcde", then it will be "bcdea" after one shift.

Example 1:

Input: s = "abcde", goal = "cdeab"
Output: true

Example 2:

Input: s = "abcde", goal = "abced"
Output: false

Explaination

First we will check the string length and the goal string length is same or not. It should be same.

Next, addition the both string and the goal. If the rotation is possible the goal will present as substring in the addition.

var rotateString = function(s, goal) {
    if(s.length!==goal.length){
        return false
    }
    let fullStr = s+s;
    return fullStr.includes(goal);
};
0
Subscribe to my newsletter

Read articles from Sk Shoyeb directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sk Shoyeb
Sk Shoyeb

Ex Full Stack Developer at @WiseBoxs | Vue React Node | MERN