★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-mjggmysq-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
We are given two strings, A
and B
.git
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
.github
Example 1: Input: A = 'abcde', B = 'cdeab' Output: true Example 2: Input: A = 'abcde', B = 'abced' Output: false
Note:微信
A
and B
will have length at most 100
.給定兩個字符串, A
和 B
。app
A
的旋轉操做就是將 A
最左邊的字符移動到最右邊。 例如, 若 A = 'abcde'
,在移動一次以後結果就是'bcdea'
。若是在若干次旋轉操做以後,A
能變成B
,那麼返回True
。spa
示例 1: 輸入: A = 'abcde', B = 'cdeab' 輸出: true 示例 2: 輸入: A = 'abcde', B = 'abced' 輸出: false
注意:code
A
和 B
長度不超過 100
。1 class Solution { 2 func rotateString(_ A: String, _ B: String) -> Bool { 3 if A.isEmpty && B.isEmpty {return true} 4 if A.isEmpty && !B.isEmpty {return false} 5 if !A.isEmpty && B.isEmpty {return false} 6 return A.count == B.count && (A + A).contains(B) 7 } 8 }
4mshtm
1 class Solution { 2 func rotateString(_ A: String, _ B: String) -> Bool { 3 guard A.count == B.count else { return false } 4 guard !A.isEmpty && !B.isEmpty else { return true } 5 return (A + A).contains(B) 6 } 7 }
8msblog
1 class Solution { 2 func rotateString(_ A: String, _ B: String) -> Bool { 3 4 if A.count == 0 && B.count == 0 { 5 return true 6 } 7 8 var A = A 9 10 for _ in 0..<A.count { 11 12 if A == B { 13 return true 14 } 15 16 let index = A.index(A.startIndex, offsetBy: 0) 17 A.append(A[index]) 18 A.removeFirst() 19 } 20 21 return false 22 } 23 }
16msrem
1 class Solution { 2 func rotateString(_ A: String, _ B: String) -> Bool { 3 4 guard A.count == B.count else { 5 return false 6 } 7 8 var A = A 9 10 for _ in 0..<A.count where A != B { 11 A.append(A.removeFirst()) 12 } 13 14 return A == B 15 } 16 }
20016kb
1 class Solution { 2 func rotateString(_ A: String, _ B: String) -> Bool { 3 guard A.length == B.length else { return false } 4 guard A != B else { return true } 5 guard B.length > 0 else { return false } 6 guard A.length > 0 else { return false } 7 8 let chars = Array(A).map({ String($0) }) 9 let n = chars.count 10 var fullRotation = [String](repeating:" ", count: 2 * n - 1) 11 12 for i in 0..<n { 13 fullRotation[i + n - 1] = chars[i] 14 } 15 for i in (1..<n).reversed() { 16 fullRotation[i - 1] = chars[i] 17 } 18 19 return fullRotation.joined().contains(B) 20 } 21 }