javascript
\nhere i used sliding window approach\n\ni will use two pointers slow and fast.\nstack to keep track of last encountered vowel.\nand also a set to keep watch if
```\nhere i used sliding window approach\n\ni will use two pointers slow and fast.\nstack to keep track of last encountered vowel.\nand also a set to keep watch if set has all 5 vowels(which means we have a valid window)\nmap each vowel in increamenting order.\n\nnow start moving fast pointer\n\nthere are 3 things possilbe.\n\n1st \nstack is empty so just add given vowel\n\n2nd\nthe new char which fast pointer is pointing is wether equal or has more value than vowel on top of stack than just add \nvowel to both stack and set\ncheck if set has all 5 vowels and calculate maxlength.\n\n3rd\nwe have now encountered a vowel which has less value than top of stack which means we hit our window \nso 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\'\n 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. \nagain check wether set has all 5 vowls or not and cal. maxlength. and clear both stack and set.\n\n(sorry for my poor english and upvote if found helpful or downvote if felt confusing :) )\n\nvar longestBeautifulSubstring = function(word) {\n  let maxLength = 0;\n  let stack = [];\n  let map = new Map();\n  let set = new Set();\n  let slow = 0,\n      fast = 0;\n\n\n  map.set("a", 1);\n  map.set("e", 2);\n  map.set("i", 3);\n  map.set("o", 4);\n  map.set("u", 5);\n\n\n  while (fast < word.length) {\n\n    let char = word[fast];\n    if (stack.length === 0) {\n\n      stack.push(char);\n      set.add(char);\n\n    } else if (map.get(char) >= map.get(stack[stack.length - 1])) {\n\n      stack.push(char);\n      set.add(char);\n      if (set.size === 5) {\n        maxLength = Math.max(maxLength, fast - slow + 1);\n\n      }\n    } else {\n\n      slow = fast;\n      fast--; \n\n      if (set.size === 5) {\n        maxLength = Math.max(maxLength, fast - slow + 1);\n      }\n\n      stack = [];\n      set.clear();\n\n    }\n\n    fast++;\n\n  }\n  return maxLength;\n};\n\n```
['Sliding Window', 'JavaScript']