Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Kaggle
- leetcode
- gs25
- Git
- 편스토랑
- hackerrank
- 더현대서울 맛집
- 캐치카페
- PYTHON
- SW Expert Academy
- Baekjoon
- 프로그래머스 파이썬
- AI 경진대회
- 파이썬
- 우분투
- 백준
- 맥북
- 프로그래머스
- 금융문자분석경진대회
- Real or Not? NLP with Disaster Tweets
- github
- 자연어처리
- dacon
- ChatGPT
- Docker
- 편스토랑 우승상품
- 데이콘
- programmers
- 코로나19
- ubuntu
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 541. Reverse String II (Python) 본문
728x90
반응형
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less 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.
Example:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
Restrictions:
- The string consists of lower English letters only.
- Length of the given string and k will in the range [1, 10000]
Solution
class Solution:
def reverseStr(self, s, k):
list_s = list(s)
for s in range(0, len(s), 2*k):
list_s[s:s+k] = list_s[s:s+k][::-1]
return "".join(list_s)
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 169. Majority Element (Python) (0) | 2020.06.23 |
---|---|
[leetCode] 507. Perfect Number (Python) (0) | 2020.06.22 |
[HackerRank] Sorting: Comparator (Python) (0) | 2020.06.20 |
[leetCode] 633. Sum of Square Numbers (Python) (0) | 2020.06.19 |
[leetCode] 283. Move Zeroes (Python) (0) | 2020.06.19 |
Comments