-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-substring-of-all-vowels-in-order_1839.py
73 lines (55 loc) · 2.45 KB
/
longest-substring-of-all-vowels-in-order_1839.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# A string is considered beautiful if it satisfies the following conditions:
# Each of the 5 English vowels ('a', 'e', 'i', 'o', 'u') must appear at least once in it.
# The letters must be sorted in alphabetical order (i.e. all 'a's before 'e's, all 'e's before 'i's, etc.).
# For example, strings "aeiou" and "aaaaaaeiiiioou" are considered beautiful, but "uaeio", "aeoiu", and "aaaeeeooo" are not beautiful.
# Given a string word consisting of English vowels, return the length of the longest beautiful substring of word. If no such substring exists, return 0.
# A substring is a contiguous sequence of characters in a string.
# Example 1:
# Input: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
# Output: 13
# Explanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
# Example 2:
# Input: word = "aeeeiiiioooauuuaeiou"
# Output: 5
# Explanation: The longest beautiful substring in word is "aeiou" of length 5.
# Example 3:
# Input: word = "a"
# Output: 0
# Explanation: There is no beautiful substring, so return 0.
# ---------------------------------------Runtime 493 ms Beats 79.77% Memory 25.4 MB Beats 7.51%---------------------------------------
# My solution
# Time Complexity O(n)
class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
ans: int = 0
_hash: dict = {"a": "e", "e": "i", "i": "o", "o": "u", "u": "u"}
left = word.find("a")
prev: str = word[0]
if len(set(word).intersection({"a", "e", "i", "o", "u"})) < 5:
return ans
if left > 0:
prev: str = word[left]
substring: list = list(word[left : left + 1])
for right in range(left + 1, len(word)):
if word[right] == prev or _hash.get(prev) == word[right]:
substring.append(word[right])
if substring[0] == "a" and substring[-1] == "u":
ans = max(ans, len(substring))
else:
left = right
substring = list(word[left : right + 1])
prev = word[right]
return ans
# Solution @MtDeity
class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
seen = set()
lo, longest = -1, 0
for hi, c in enumerate(word):
if hi > 0 and c < word[hi - 1]:
seen = set()
lo = hi - 1
seen.add(c)
if len(seen) == 5:
longest = max(longest, hi - lo)
return longest