https://leetcode.com/problems/reverse-only-letters/description/
Reverse Only Letters - LeetCode
Can you solve this real interview question? Reverse Only Letters - Given a string s, reverse the string according to the following rules: * All the characters that are not English letters remain in the same position. * All the English letters (lowercase or
leetcode.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Solution:
def reverseOnlyLetters(self, s: str) -> str:
s = list(s)
left, right = 0, len(s) - 1
while left <= right:
if s[left].isalpha() and s[right].isalpha():
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
elif not s[left].isalpha():
left += 1
elif not s[right].isalpha():
right -= 1
return(''.join(s))
|
cs |
투포인터 개념으로 양끝 에서부터 체크하면됩니당
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2913. Subarrays Distinct Element Sum of Squares I (0) | 2024.01.15 |
---|---|
leetcode 3005. Count Elements With Maximum Frequency (0) | 2024.01.15 |
leetcode 1304. Find N Unique Integers Sum up to Zero (0) | 2024.01.10 |
leetcode 1636. Sort Array by Increasing Frequency (0) | 2024.01.09 |
leetcode 2278. Percentage of Letter in String (0) | 2024.01.08 |
댓글