Skip to content

Commit d135248

Browse files
committed
Time: 215 ms (50%), Space: 22.7 MB (0%) - LeetHub
1 parent ffbf90b commit d135248

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
class Solution:
4+
def reverseWords(self, s: str) -> str:
5+
vowels = set("aeiou")
6+
words = s.split()
7+
targetCount = sum(1 for c in words[0] if c in vowels)
8+
for i in range(1, len(words)):
9+
vowelCount = sum(1 for c in words[i] if c in vowels)
10+
if vowelCount == targetCount:
11+
words[i] = words[i][::-1]
12+
return " ".join(words)
13+
14+
15+
s = "cat and mice"
16+
print(Solution().reverseWords(s))
17+
s = "book is nice"
18+
print(Solution().reverseWords(s))
19+
s = "banana healthy"
20+
print(Solution().reverseWords(s))

0 commit comments

Comments
 (0)