↓快轉到主要內容
  1. LeetCode/

LeetCode 796: Rotate String

·1 分鐘· ·
LeetCode Easy String String-Matching
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試:2022-09-07

  • 提交紀錄:Runtime Error ×2 → Wrong Answer → Accepted ×5

解題思路
#

s + s 包含了 s 所有的旋轉(每個起點往後取 len(s) 個字元)。所以長度相同而且 goal in s + s 就成立(String 模板二)。長度檢查不能省:"aa" 也在 "a" + "a" 裡。

我的嘗試
#

這題有兩個「AC 但其實是錯的」版本:

  • 前兩版用 s.index 找字元的位置比下一個字元,i + 1 超出範圍(IndexError)。
  • 接著改成「s 的每兩個相鄰字元都要出現在 goal 裡,最多允許一個例外」,第一版 WA 之後加上長度檢查就 AC 了,但這個想法本身是錯的:s = "b"、goal = "a" 只有一組片段 "b" 不在裡面,還在允許範圍內,回傳 True。
  • 之後改成 goal in s + s,但長度只檢查了 len(goal) < len(s),也 AC 了:s = "a"、goal = "aa" 會回傳 True。下一版補上 len(goal) == len(s) 才是正確的。

正確解法
#

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

複雜度
#

時間 O(n²)(內建搜尋最壞情況),用 KMP 可以 O(n);空間 O(n)。

要特別避免的錯誤
#

  • 長度要相等,只檢查 len(goal) < len(s) 不夠。
  • 「相鄰兩個字元都出現在 goal 裡」不代表是旋轉。

面試口說整理
#

s 自己接自己會包含所有的旋轉,所以我檢查兩個字串長度相同,而且 goal 是 s + s 的子字串。內建搜尋最壞 O(n²),用 KMP 可以 O(n)。

相關文章

LeetCode 459: Repeated Substring Pattern
·1 分鐘
LeetCode Easy String String-Matching Z-Algorithm Knuth-Morris-Pratt-Algorithm
LeetCode 459 解題筆記:s 由某個子字串重複組成,等價於 s 出現在 (s + s) 去掉頭尾字元之後的字串裡
LeetCode 28: Find the Index of the First Occurrence in a String
·1 分鐘
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 解題筆記:在每個起點比對 needle,或直接用 str.find;index 找不到會丟例外
LeetCode 806: Number of Lines To Write String
·1 分鐘
LeetCode Easy Array String
LeetCode 806 解題筆記:模擬寫字,加上去會超過 100 就換行;剛好 100 時還留在這一行
LeetCode 771: Jewels and Stones
·1 分鐘
LeetCode Easy Hash-Table String
LeetCode 771 解題筆記:把 jewels 放進集合,數 stones 裡有幾個字元在集合裡
LeetCode 748: Shortest Completing Word
·1 分鐘
LeetCode Easy Array Hash-Table String
LeetCode 748 解題筆記:車牌只看字母並轉小寫計數,找出每個字母次數都夠的最短單字,同長度取最前面的
LeetCode 709: To Lower Case
·1 分鐘
LeetCode Easy String
LeetCode 709 解題筆記:大寫字母的 ASCII 加 32 就是小寫,其他字元不變