- Date : 2021.10.24(일)
- Time : 15분
- Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
- 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 left the other as original.
- 1 <= s.length <= 10^4
- s consists of only lowercase English letters.
- 1 <= k <= 10^4
- Input: s = "abcdefg", k = 2
- Output: "bacdfeg"
def reverseStr(self, s: str, k: int) -> str:
answer = ''
for i in range(0, len(s), k):
tmp = s[i: i + k]
if (i // k) % 2 == 0:
answer = answer + tmp[::-1]
else:
answer = answer + tmp
return answer
: k 까지의 문자는 뒤집고 그 이후는 그대로 두는 문제였다. 그래서 for문을 이용해 처음에 k까지 간 후에 그 숫자는 뒤집어주었고 그 이외의 숫자는 그대로 answer에 + 해주었다.