Skip to content

796. Rotate String 👍

  • Time: $O(|\texttt{s}|^2)$
  • Space: $O(1)$
1
2
3
4
5
6
class Solution {
 public:
  bool rotateString(string s, string goal) {
    return s.length() == goal.length() && (s + s).find(goal) != string::npos;
  }
};
1
2
3
4
5
class Solution {
  public boolean rotateString(String s, String goal) {
    return s.length() == goal.length() && (s + s).contains(goal);
  }
}
1
2
3
class Solution:
  def rotateString(self, s: str, goal: str) -> bool:
    return len(s) == len(goal) and goal in s + s