Skip to content

Commit

Permalink
Modify lc345
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanData committed Sep 23, 2023
1 parent 73b6e3b commit a8d2d61
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
26 changes: 12 additions & 14 deletions lc/lc345ReverseVowelsofaString.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ func reverseVowels(inputString string) string {

// looping until the left pointer is less than the right pointer
for leftPointer < rightPointer {
// finding the index of the first vowel from the left
for leftPointer < rightPointer && !isVowel(runes[leftPointer]) {
if leftPointer < rightPointer && !isVowel(runes[leftPointer]) {
// finding the index of the first vowel from the left
leftPointer += 1
}
// finding the index of the first vowel from the right
for rightPointer > leftPointer && !isVowel(runes[rightPointer]) {
} else if rightPointer > leftPointer && !isVowel(runes[rightPointer]) {
// finding the index of the first vowel from the right
rightPointer -= 1
}

// swapping the vowels
runes[leftPointer], runes[rightPointer] = runes[rightPointer], runes[leftPointer]
} else {
// swapping the vowels
runes[leftPointer], runes[rightPointer] = runes[rightPointer], runes[leftPointer]

// moving the pointers inward
leftPointer += 1
rightPointer -= 1
// moving the pointers inward
leftPointer += 1
rightPointer -= 1
}
}
// converting the slice of runes back to a string
return string(runes)
return string(runes) // converting the slice of runes back to a string
}
14 changes: 7 additions & 7 deletions py/345. Reverse Vowels of a String.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ class Solution:
def reverseVowels(self, s: str) -> str:
lst = list(s)
n = len(s)
vowels = list("aeiouAEIOU")
vowels = "aeiouAEIOU"
l, r = 0, n - 1
while l < r:
while l < r and lst[l] not in vowels:
if l < r and lst[l] not in vowels:
l += 1
elif r > l and lst[r] not in vowels:
r -= 1
else:
lst[l], lst[r] = lst[r], lst[l] # swapping the vowels
l += 1
while r > l and lst[r] not in vowels:
r -= 1
lst[l], lst[r] = lst[r], lst[l] # swapping the vowels

l += 1
r -= 1
return "".join(lst)


Expand Down

0 comments on commit a8d2d61

Please sign in to comment.