# 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

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/string/easy/796.-rotate-string.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
