Skip to content

Commit 664954e

Browse files
author
Albert Hu
authored
Merge pull request #24 from alberthu16/day35
Day 35: reverse string pt 2
2 parents 322dace + f8d38bd commit 664954e

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

day35/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Question of the day: https://leetcode.com/problems/reverse-string-ii/#/description
2+
3+
Given a string and an integer k, you need to reverse the first k characters
4+
for every 2k characters counting from the start of the string. If there are
5+
less than k characters left, reverse all of them. If there are less than
6+
2k but greater than or equal to k characters, then reverse the first k
7+
characters and leave the rest as they are.
8+
9+
Example:
10+
11+
```
12+
Input: s = "abcdefg", k = 2
13+
Output: "bacdfeg"
14+
```
15+
16+
Restrictions:
17+
18+
1. The string consists of lowercase English letters only.
19+
2. Length of the given string and k will in the range [1, 10000]
20+
21+
## Ideas
22+
23+
1. break the sting into chunks of length `2k`
24+
2. reverse up to the first `k` of each chunk
25+
3. join the chunks together and return the result
26+
27+
28+
Although I logically want to break the string up into `2k` chunks right away,
29+
I can just use some pointers as I move down the string so that I don't allocate
30+
too much unnecessary memory. Reversing the relevant indices and returning at the
31+
end will take `O(n)` runtime and `O(1)` space.
32+
33+
## Code
34+
[Python](./reverseStr.py)
35+
36+
## Follow-up
37+
38+
Try writing this in C.

day35/reverseStr.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
def reverseStr(s, k):
2+
# edge
3+
size = len(s)
4+
if size < k:
5+
return s[::-1]
6+
7+
# convert string to list
8+
letters = list(s)
9+
i = 0
10+
11+
# reverse all the full chunks
12+
while i + 2*k-1 < size:
13+
reverse(letters, i, i+k-1)
14+
i += 2*k
15+
16+
# take care of the remainder
17+
if i+k-1 < size:
18+
reverse(letters, i, i+k-1)
19+
else:
20+
reverse(letters, i, size-1)
21+
22+
# fin
23+
return "".join(letters)
24+
25+
# reverses the indices of arr between start and end, in place
26+
def reverse(arr, start, end):
27+
while start < end:
28+
arr[start], arr[end] = arr[end], arr[start]
29+
start += 1
30+
end -= 1
31+
32+
def testReverse():
33+
t1 = [0, 1, 2, 3, 4]
34+
reverse(t1, 0, 2)
35+
assert t1 == [2, 1, 0, 3, 4]
36+
37+
t2 = [0, 1, 2, 3, 4]
38+
reverse(t2, 1, 3)
39+
assert t2 == [0, 3, 2, 1, 4]
40+
41+
def testReverseStr():
42+
assert reverseStr("", 1) == ""
43+
assert reverseStr("", 2) == ""
44+
assert reverseStr("", 10000) == ""
45+
assert reverseStr("abcdefg", 1) == "abcdefg"
46+
assert reverseStr("abcdefg", 2) == "bacdfeg"
47+
assert reverseStr("abcdefg", 3) == "cbadefg"
48+
assert reverseStr("abcdefg", 4) == "dcbaefg"
49+
assert reverseStr("abcdefg", 5) == "edcbafg"
50+
assert reverseStr("abcdefg", 6) == "fedcbag"
51+
assert reverseStr("abcdefg", 7) == "gfedcba"
52+
assert reverseStr("abcdefg", 8) == "gfedcba"
53+
assert reverseStr("abcdefg", 9) == "gfedcba"
54+
55+
def main():
56+
testReverse()
57+
testReverseStr()
58+
59+
if __name__ == "__main__":
60+
main()

0 commit comments

Comments
 (0)