796. Rotate String旋轉字符串
阿新 • • 發佈:2018-08-11
public () ber The bce 正常 dea cor 思維
[抄題]:
We are given two strings, A
and B
.
A shift on A
consists of taking string A
and moving the leftmost character to the rightmost position. For example, if A = ‘abcde‘
, then it will be ‘bcdea‘
after one shift on A
. Return True
if and only if A
can become B
after some number of shifts on A
.
Example 1: Input: A = ‘abcde‘, B = ‘cdeab‘ Output: true Example 2: Input: A = ‘abcde‘, B = ‘abced‘ Output: false
[暴力解法]:
時間分析:
空間分析:
[優化後]:
時間分析:
空間分析:
[奇葩輸出條件]:
[奇葩corner case]:
[思維問題]:
以為要用kmp:其實有時候簡單拼一下就行了
[英文數據結構或算法,為什麽不用別的數據結構或算法]:
[一句話思路]:
[輸入量]:空: 正常情況:特大:特小:程序裏處理到的特殊情況:異常情況(不合法不合理的輸入):
[畫圖]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分鐘肉眼debug的結果]:
[總結]:
[復雜度]:Time complexity: O(1) Space complexity: O(1)
[算法思想:叠代/遞歸/分治/貪心]:
[關鍵模板化代碼]:
[其他解法]:
[Follow Up]:
[LC給出的題目變變變]:
[代碼風格] :
[是否頭一次寫此類driver funcion的代碼] :
[潛臺詞] :
public boolean rotateString(String A, String B) { return A.length() == B.length() && (A + A).contains(B); }
796. Rotate String旋轉字符串