# 796. Rotate String

## Intuition

To check if one string is a rotation of another, we can **double the original string**. If the rotated version exists, it **must appear as a substring** within this doubled string.

## Complexity

| Space Complexity | Time Complexity   |
| ---------------- | ----------------- |
| $$\text{O}(N)$$  | $$\text{O}(N^2)$$ |

## Code

```java
public boolean rotateString(String original, String target) {
    // Edge case: if lengths differ, rotation is not possible
    if (original.length() != target.length()) return false;

    // Create a doubled version of the original string
    String doubledOriginal = original + original;
    int originalLength = original.length();

    // Check all substrings of length originalLength in doubledOriginal
    for (int i = 0; i < originalLength; ++i) {
        // Extract substring and compare it with target
        if (doubledOriginal.substring(i, i + originalLength).equals(target)) {
            return true; // Found a match; it's a valid rotation
        }
    }

    return false; // No valid rotation found
}

```


---

# 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://private-26.gitbook.io/notes/coding/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.
