Skip to main content

Command Palette

Search for a command to run...

Rotate String Leetcode Question Solve - Javascript DSA Question

Updated
•1 min read•View as Markdown
Rotate String Leetcode Question Solve - Javascript DSA Question
S

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

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);
};