796. Rotate String LeetCode Solution
In this guide, you will get 796. Rotate String LeetCode Solution with the best time and space complexity. The solution to Rotate String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Rotate String solution in C++
- Rotate String solution in Java
- Rotate String solution in Python
- Additional Resources
Problem Statement of Rotate String
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.
Example 1:
Input: s = “abcde”, goal = “cdeab”
Output: true
Example 2:
Input: s = “abcde”, goal = “abced”
Output: false
Constraints:
1 <= s.length, goal.length <= 100
s and goal consist of lowercase English letters.
Complexity Analysis
- Time Complexity: O(|\texttt{s}|^2)
- Space Complexity: O(1)
796. Rotate String LeetCode Solution in C++
class Solution {
public:
bool rotateString(string s, string goal) {
return s.length() == goal.length() && (s + s).find(goal) != string::npos;
}
};
/* code provided by PROGIEZ */
796. Rotate String LeetCode Solution in Java
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}
// code provided by PROGIEZ
796. Rotate String LeetCode Solution in Python
class Solution:
def rotateString(self, s: str, goal: str) -> bool:
return len(s) == len(goal) and goal in s + s
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.