forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest Substring Of All Vowels in Order.js
79 lines (55 loc) · 1.92 KB
/
Longest Substring Of All Vowels in Order.js
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
74
75
76
77
78
here i used sliding window approach
i will use two pointers slow and fast.
stack to keep track of last encountered vowel.
and also a set to keep watch if set has all 5 vowels(which means we have a valid window)
map each vowel in increamenting order.
now start moving fast pointer
there are 3 things possilbe.
1st
stack is empty so just add given vowel
2nd
the new char which fast pointer is pointing is wether equal or has more value than vowel on top of stack than just add
vowel to both stack and set
check if set has all 5 vowels and calculate maxlength.
3rd
we have now encountered a vowel which has less value than top of stack which means we hit our window
so move slow pointer to fast and also decrease fast bcz lets say in this given ex. 'a e i o u a a a i'
when fast pointer is at 5th index on line 66 fast pointer will get incremented and hence index 5 will not get processed and won't be added to stack and set.
again check wether set has all 5 vowls or not and cal. maxlength. and clear both stack and set.
(sorry for my poor english and upvote if found helpful or downvote if felt confusing :) )
var longestBeautifulSubstring = function(word) {
let maxLength = 0;
let stack = [];
let map = new Map();
let set = new Set();
let slow = 0,
fast = 0;
map.set("a", 1);
map.set("e", 2);
map.set("i", 3);
map.set("o", 4);
map.set("u", 5);
while (fast < word.length) {
let char = word[fast];
if (stack.length === 0) {
stack.push(char);
set.add(char);
} else if (map.get(char) >= map.get(stack[stack.length - 1])) {
stack.push(char);
set.add(char);
if (set.size === 5) {
maxLength = Math.max(maxLength, fast - slow + 1);
}
} else {
slow = fast;
fast--;
if (set.size === 5) {
maxLength = Math.max(maxLength, fast - slow + 1);
}
stack = [];
set.clear();
}
fast++;
}
return maxLength;
};