September 2021 Leetcode Challenge Leetcode - Reverse Only Letters #917 Difficulty: Easy
Пікірлер: 3
@chamikaonyt Жыл бұрын
This was what I came up with. :) def reverseOnlyLetters(self, s: str) -> str: i = 0 j = len(s) - 1 answer = "" while i < len(s): if not s[i].isalpha(): answer += s[i] i += 1 elif s[i].isalpha() and s[j].isalpha(): answer += s[j] j -= 1 i +=1 elif s[i].isalpha() and not s[j].isalpha(): j -= 1 return answer