aboutsummaryrefslogtreecommitdiff
path: root/find_longest_awesome_substr.py
blob: d4113d6deed58504f9ec1cb7fbee7020f1bb98af (plain)
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
# 1542. Find Longest Awesome Substring

"""
you are given a string 's'. an awesome substring is a non-empty substring of
's' such that we can make any number of swaps in order to make it a
palindrome. return the length of the maximum length awesome substring of 's'.
"""


class Solution(object):
    def longestAwesome(self, s):
        """
        :type s: str
        :rtype: int
        """
        ans, mask = 0, 0
        dp = [-1] + [len(s)] * 1023
        for i in range(len(s)):
            mask ^= 1 << (ord(s[i]) - 48)
            for j in range(11):
                check = 1023 & (mask ^ (1 << j))
                ans = max(ans, i - dp[check])
            dp[mask] = min(dp[mask], i)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.longestAwesome(s="3242415"))
    print(obj.longestAwesome(s="12345678"))
    print(obj.longestAwesome(s="213123"))