↓Skip to main content
  1. LeetCode/

LeetCode 796: Rotate String

·2 mins· ·
LeetCode Easy String String-Matching
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-07

  • Submissions: Runtime Error ×2 → Wrong Answer → Accepted ×5

Approach
#

s + s contains every rotation of s (each start followed by len(s) characters). So the answer is equal lengths and goal in s + s (String Template 2). The length check is required: "aa" is inside "a" + "a" too.

My Attempts
#

This problem had two versions that were accepted yet wrong:

  • The first two used s.index to locate characters and compare the next one, running i + 1 out of range (IndexError).
  • Then came “every adjacent pair of s must appear in goal, with at most one exception”; after one WA, adding a length check was accepted, but the idea is wrong: with s = "b", goal = "a" the single missing piece "b" is within the allowance, so it returns True.
  • Next, goal in s + s with only a len(goal) < len(s) check was accepted too: s = "a", goal = "aa" returns True. The following version added len(goal) == len(s), which is actually correct.

Solution
#

class Solution:
    def rotateString(self, s: str, goal: str) -> bool:
        return len(s) == len(goal) and goal in s + s

Complexity
#

Time O(n²) worst case for the built-in search, O(n) with KMP; space O(n).

Mistakes to Avoid
#

  • The lengths must be equal; checking only len(goal) < len(s) is not enough.
  • “Every adjacent pair appears in goal” does not imply a rotation.

Interview Explanation
#

s concatenated with itself contains every rotation, so I check equal lengths and that goal is a substring of s + s. The built-in search is O(n²) worst case; KMP makes it O(n).

Related

LeetCode 459: Repeated Substring Pattern
·1 min
LeetCode Easy String String-Matching Z-Algorithm Knuth-Morris-Pratt-Algorithm
LeetCode 459 note: s is a repetition of a substring exactly when s occurs in s + s with its first and last characters removed
LeetCode 28: Find the Index of the First Occurrence in a String
·1 min
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 note: try matching needle at every start, or use str.find; index raises when not found
LeetCode 806: Number of Lines To Write String
·2 mins
LeetCode Easy Array String
LeetCode 806 note: simulate writing, wrapping when the next letter would exceed 100; landing exactly on 100 stays on the line
LeetCode 771: Jewels and Stones
·1 min
LeetCode Easy Hash-Table String
LeetCode 771 note: put jewels in a set and count how many stones are in it
LeetCode 748: Shortest Completing Word
·2 mins
LeetCode Easy Array Hash-Table String
LeetCode 748 note: count the plate’s letters in lowercase, then find the shortest word covering every count, taking the earliest on ties
LeetCode 709: To Lower Case
·1 min
LeetCode Easy String
LeetCode 709 note: an uppercase letter plus 32 in ASCII is its lowercase form; other characters stay