forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseStringII.swift
39 lines (31 loc) · 994 Bytes
/
ReverseStringII.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Question Link: https://leetcode.com/problems/reverse-string-ii/
* Primary idea: Use reversed() to help reverse the string
*
* Note: Avoid index out of range error as k will pass the right edge of the string
*
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class ReverseStringII {
func reverseStr(_ s: String, _ k: Int) -> String {
var chars = [Character](s.characters), res = [Character]()
if k < 0 {
fatalError("Invalid k")
}
for i in stride(from: 0, to: chars.count, by: 2 * k) {
print(i)
if chars.count < i + k {
res += chars[i..<chars.count].reversed()
break
}
res += chars[i..<i + k].reversed()
if chars.count < i + 2 * k {
res += chars[i + k..<chars.count]
break
}
res += chars[i + k..<i + 2 * k]
}
return String(res)
}
}