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
- 편스토랑
- 금융문자분석경진대회
- gs25
- programmers
- 더현대서울 맛집
- 맥북
- SW Expert Academy
- Real or Not? NLP with Disaster Tweets
- AI 경진대회
- 파이썬
- Git
- leetcode
- 편스토랑 우승상품
- 데이콘
- Docker
- 코로나19
- Baekjoon
- ChatGPT
- 프로그래머스 파이썬
- dacon
- ubuntu
- 프로그래머스
- Kaggle
- 캐치카페
- hackerrank
- PYTHON
- github
- 자연어처리
- 백준
- 우분투
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 917. Reverse Only Letters (Python) 본문
728x90
반응형
Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.
Example 1:
Input: "ab-cd"
Output: "dc-ba"
Example 2:
Input: "a-bC-dEf-ghIj"
Output: "j-Ih-gfE-dCba"
Example 3:
Input: "Test1ng-Leet=code-Q!"
Output: "Qedo1ct-eeLg=ntse-T!"
Note:
- S.length <= 100
- 33 <= S[i].ASCIIcode <= 122
- S doesn't contain \ or "
Solution
class Solution:
def reverseOnlyLetters(self, S: str) -> str:
temp = []
indexs = {}
answer = []
cnt = 0
s_list = list(S)
for i in range(len(s_list)):
if s_list[i].isalpha() == True:
temp.append(s_list[i])
cnt = cnt + 1
if s_list[i].isalpha() == False:
indexs[i] = s_list[i]
if cnt == len(S):
answer = S[::-1]
else:
keys = list(indexs.keys())
temp = temp[::-1]
idx = 0
for i in range(len(S)):
if i in keys:
answer.append(indexs[i])
elif i not in keys:
answer.append(temp[idx])
idx = idx + 1
answer = ''.join(answer)
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1507. Reformat Date (Python) (0) | 2020.09.06 |
---|---|
[leetCode] 415. Add Strings (Python) (0) | 2020.09.05 |
[leetCode] 443. String Compression (Python) (0) | 2020.09.03 |
[leetCode] 1480. Running Sum of 1d Array (Python) (0) | 2020.09.02 |
[leetCode] 728. Self Dividing Numbers (Python) (0) | 2020.09.01 |
Comments