Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit adcfffd

Browse files
D. J.:
- Added the leetcode problem and solution for 2785
1 parent 53816ae commit adcfffd

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@
294294
- [2390 Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/description/)
295295
- [2466 Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/description/)
296296
- [2523 Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/description/)
297+
- [2785 Sort Vowels in a String](https://leetcode.com/problems/sort-vowels-in-a-string/description/)
297298
- [2799 Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/)
298299
- [3136 Valid Word](https://leetcode.com/problems/valid-word/description/)
299300
- [3202 Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/description/)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def sortVowels(self, s: str) -> str:
5+
"""
6+
Given a 0-indexed string s, permute s to get a new string t such that:
7+
- All consonants remain in their original places. More formally, if there is an
8+
index i with 0 <= i < s.length such that s[i] is a consonant, then t[i] = s[i].
9+
- The vowels must be sorted in the nondecreasing order of their ASCII values.
10+
More formally, for pairs of indices i, j with 0 <= i < j < s.length such that
11+
s[i] and s[j] are vowels, then t[i] must not have a higher ASCII value than
12+
t[j].
13+
14+
Return the resulting string.
15+
16+
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in lowercase or
17+
uppercase. Consonants comprise all letters that are not vowels.
18+
"""
19+
# Get the vowels and position of them
20+
vowels = [c for c in s if c in "aeiouAEIOU"]
21+
22+
# Sort the vowels
23+
vowels.sort()
24+
25+
# Return the resulting string with sorted vowels
26+
result = []
27+
j = 0
28+
for c in s:
29+
if c in "aeiouAEIOU":
30+
result.append(vowels[j])
31+
j += 1
32+
else:
33+
result.append(c)
34+
return "".join(result)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._2785_sort_vowels_in_a_string import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "expected"],
8+
argvalues=[
9+
("lEetcOde", "lEOtcede"),
10+
("lYmpH", "lYmpH"),
11+
],
12+
)
13+
def test_func(s: str, expected: str):
14+
"""Tests the solution of a LeetCode problem."""
15+
sorted_s = Solution().sortVowels(s)
16+
assert sorted_s == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /