Skip to content

Commit 74ea41a

Browse files
chore: add LeetCode daily solution
1 parent f12f451 commit 74ea41a

5 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Rotate String (Easy)
2+
3+
**Problem ID:** 796
4+
**Date:** 2026-05-03
5+
**Link:** https://leetcode.com/problems/rotate-string/
6+
7+
## Approach
8+
9+
To solve the "Rotate String" problem, the key idea is to leverage the properties of string rotations. Specifically, if string `s` can be transformed into string `goal` through a series of left shifts, then `goal` must appear as a substring within the concatenated string `s + s`.
10+
11+
### Approach:
12+
13+
1. **Concatenation**: Create a new string by concatenating `s` with itself (i.e., `s + s`). This new string contains all possible rotations of `s` as contiguous substrings. For example, if `s = "abcde"`, then `s + s = "abcdeabcde"` will contain all rotations: "abcde", "bcdea", "cdeab", "deabc", and "eabcd".
14+
15+
2. **Substring Check**: Check if `goal` is a substring of the concatenated string `s + s`. If it is, then `s` can be rotated to form `goal`, and we return `true`. If not, we return `false`.
16+
17+
### Data Structures:
18+
- Strings are used to hold the input and the concatenated result. No additional complex data structures are necessary for this approach.
19+
20+
### Complexity:
21+
- **Time Complexity**: The solution primarily involves string concatenation and substring searching. The concatenation takes O(n) time, where n is the length of `s`. The substring search can be done in O(n) time as well, resulting in an overall time complexity of O(n).
22+
- **Space Complexity**: The space complexity is O(n) due to the storage of the concatenated string `s + s`.
23+
24+
### Summary:
25+
This approach is efficient and straightforward, utilizing the properties of string manipulation to determine if one string can be obtained from another through rotations. By checking for the presence of `goal` in the doubled string, we can quickly conclude whether the transformation is possible.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public boolean rotateString(String s, String goal) {
3+
if (s.length() != goal.length()) {
4+
return false;
5+
}
6+
String concatenated = s + s;
7+
return concatenated.contains(goal);
8+
}
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var rotateString = function(s, goal) {
2+
if (s.length !== goal.length) return false;
3+
return (s + s).includes(goal);
4+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def rotateString(self, s: str, goal: str) -> bool:
3+
if len(s) != len(goal):
4+
return False
5+
return goal in (s + s)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
442442
- 2026-04-30 — [Maximum Path Score in a Grid](https://leetcode.com/problems/maximum-path-score-in-a-grid/) (Medium) → `Medium/2026-04-30-3742-Maximum-Path-Score-in-a-Grid`
443443
- 2026-05-01 — [Rotate Function](https://leetcode.com/problems/rotate-function/) (Medium) → `Medium/2026-05-01-396-Rotate-Function`
444444
- 2026-05-02 — [Rotated Digits](https://leetcode.com/problems/rotated-digits/) (Medium) → `Medium/2026-05-02-788-Rotated-Digits`
445+
- 2026-05-03 — [Rotate String](https://leetcode.com/problems/rotate-string/) (Easy) → `Easy/2026-05-03-796-Rotate-String`

0 commit comments

Comments
 (0)