[Swift]LeetCode796. 旋转字符串 | Rotate String
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10547242.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
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
Note:
A
andB
will have length at most100
.
给定两个字符串, A
和 B
。
A
的旋转操作就是将 A
最左边的字符移动到最右边。 例如, 若 A = 'abcde'
,在移动一次之后结果就是'bcdea'
。如果在若干次旋转操作之后,A
能变成B
,那么返回True
。
示例 1: 输入: A = 'abcde', B = 'cdeab' 输出: true 示例 2: 输入: A = 'abcde', B = 'abced' 输出: false
注意:
A
和B
长度不超过100
。
Runtime: 4 ms
Memory Usage: 20.2 MB
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 }
4ms
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 }
8ms
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 }
16ms
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了