Skip to content

Commit 1e2e5b0

Browse files
committed
Reverse string II.
1 parent f65c7ed commit 1e2e5b0

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
3+
4+
If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
5+
6+
 
7+
8+
Example 1:
9+
Input: s = "abcdefg", k = 2
10+
Output: "bacdfeg"
11+
12+
Example 2:
13+
Input: s = "abcd", k = 2
14+
Output: "bacd"
15+
 
16+
17+
Constraints:
18+
- 1 <= s.length <= 10^4
19+
- s consists of only lowercase English letters.
20+
- 1 <= k <= 10^4
21+
*/
22+
class Solution {
23+
func reverseStr(_ s: String, _ k: Int) -> String {
24+
if s.count <= k {
25+
return String(s.reversed())
26+
} else {
27+
var result = s
28+
let quotient = s.count / (2 * k)
29+
let remiander = s.count % (2 * k)
30+
if quotient > 0 {
31+
for i in 0..<quotient {
32+
let subString = String(s[s.index(s.startIndex, offsetBy: i * 2 * k)..<s.index(s.startIndex, offsetBy: i * 2 * k + k)].reversed())
33+
result.replaceSubrange(s.index(s.startIndex, offsetBy: i * 2 * k)..<s.index(s.startIndex, offsetBy: i * 2 * k + k), with: subString)
34+
}
35+
}
36+
if remiander > 0 {
37+
if remiander >= k {
38+
let subString = String(s[s.index(s.startIndex, offsetBy: quotient * 2 * k)..<s.index(s.startIndex, offsetBy: quotient * 2 * k + k)].reversed())
39+
result.replaceSubrange(s.index(s.startIndex, offsetBy: quotient * 2 * k)..<s.index(s.startIndex, offsetBy: quotient * 2 * k + k), with: subString)
40+
} else {
41+
let subString = String(s[s.index(s.startIndex, offsetBy: quotient * 2 * k)...s.index(s.startIndex, offsetBy: s.count - 1)].reversed())
42+
result.replaceSubrange(s.index(s.startIndex, offsetBy: quotient * 2 * k)...s.index(s.startIndex, offsetBy: s.count - 1), with: subString)
43+
}
44+
}
45+
return result
46+
}
47+
}
48+
}
49+
50+
let s = Solution()
51+
let r = s.reverseStr("abcd", 2)
52+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/541.Reverse String II.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
103. [Fibonacci Number](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/509.Fibonacci%20Number.playground/Contents.swift)
108108
104. [Detect Capital](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/520.Detect%20Captial.playground/Contents.swift)
109109
105. [Longest Uncommon Subsequence I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/521.Longest%20Uncommon%20Subsequence%20I.playground/Contents.swift)
110+
106. [Reverse String II](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/541.Reverse%20String%20II.playground/Contents.swift)
110111

111112
#### Medium
112113

0 commit comments

Comments
 (0)