Python Sliding Window Method with Explanation and Extension
\nclass Solution:\n    def longestBeautifulSubstring(self, word):\n        n = len(word)\n        length = 0\n        order = {"a": 0, "e": 1, "i": 2, "o": 3, "
```\nclass Solution:\n    def longestBeautifulSubstring(self, word):\n        n = len(word)\n        length = 0\n        order = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4}\n        l, r = 0, 0\n        while l < n:\n            \n            # 1. Find the index of "a" after pointer l\n            if word[l] != "a":\n                l += 1\n                continue\n            # 2. Expand the window\n            r = l + 1\n            while r < n and (\n                (order[word[r - 1]] == order[word[r]])\n                or (order[word[r - 1]] + 1 == order[word[r]])\n            ):\n                r += 1\n            # Once come out of the inner while loop, check the below\n            # if condition to see if the window from l up to r-1 ending with "u"\n            if word[r - 1] == "u":\n                length = max(length, r - l)\n            # 3. reset left side of the window to check if\n            # there are further desired beautiful substring \n            l = r\n        return length\n\n```\nQuestion: What if we allow other characters to appear in word, and still ask: find longest substring which is beautiful in the sense that the substring allow non-vowels and must contain all the vowels and the vowels are in sorted order?\n\nComment: Modify the solution to get the answer.\nNote: Below code cetainly also solve leetcode 1839 which is a simpler/special case.\n```\ndef findBeautifulSubstring(word):\n    res = 0\n    order = {"a": 0, "e": 1, "i": 2, "o": 3, "u": 4}\n    # first find a\n    n = len(word)\n    i = 0\n    while i < n:\n        if word[i] != "a":\n            i += 1\n            continue\n\n        j = i + 1\n        prev_vowel = word[j - 1]\n        while j < n:\n            if word[j] not in order:\n                j += 1\n            else:\n                if (\n                    order[word[j]] == order[prev_vowel]\n                    or order[word[j]] == 1 + order[prev_vowel]\n                ):\n                    prev_vowel = word[j]\n                    j += 1\n                else:\n                    break\n\n        if prev_vowel == "u":\n            t = i - 1\n            while t >= 0 and (word[t] not in order):\n                t -= 1\n            res = max(res, j - t - 1)\n\n        i = j\n    return res\n\n\nfor word in words:\n    print(findBeautifulSubstring(word))\n\nprint(findBeautifulSubstring("xyzaccceiouyyyyaaaaaaaaaaaaaaaeiou"))  # return 23\nprint(findBeautifulSubstring("xyzaccceiouyyyyabafaaagaaaaaaaaaaeiou"))  # return 26 b/c add b, f, g in the previous length 23 substring\nprint(findBeautifulSubstring("aaaaaaaaaaaaaaaeiou"))  # return 19\nprint(findBeautifulSubstring("aeiaaioaaaaeiiiiouuuooaauuaeiu"))  # return 13\nprint(findBeautifulSubstring("aeeeiiiioooauuuaeiou"))  # return 5\nprint(findBeautifulSubstring("aeeeiiiioooauuuaeiouyao"))  # return 6\n```\n
[]