> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/string/easy/796.-rotate-string.md).

# 796. Rotate String

## Problem Statement

<br>

Given two strings `s` and `goal`, return `true` *if and only if* `s` *can become* `goal` *after some number of **shifts** on* `s`.

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.

&#x20;

**Example 1:**

<pre><code><strong>Input: s = "abcde", goal = "cdeab"
</strong><strong>Output: true
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: s = "abcde", goal = "abced"
</strong><strong>Output: false
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= s.length, goal.length <= 100`
* `s` and `goal` consist of lowercase English letters.

## Intuition

```
BruteForce:
Rotate and at each step, Check if equal
O(n^2)


Optimal
add the string to itself and try to find the other string in this one

eg: s : abcd
    t : cdab

abcd|abcd
See if string is rotated we get cdab in the concat   
```

### Links

<https://leetcode.com/problems/rotate-string/description/>

### Video Links

### Approach 1:

```
Brute
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    bool rotateString(string s, string goal) {
        
        int n=goal.size();

        for(int i=0;i<n;i++){
            if(goal==s)
                return true;


            for(int j=n-1;j>0;j--){
                swap(goal[j],goal[j-1]);
            }

            if(goal==s)
                return true;

        }

        return false;
    }
};
```

{% endcode %}

### Approach 2:

```
Optimal
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    bool rotateString(string s, string goal) {
        
        if (s.length() != goal.length())
            return false;
 
        string temp = s + s;
        return (temp.find(goal) != string::npos);

    }
};
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
