LC 1542. Find Longest Awesome Substring

link

class Solution {
public:
    int longestAwesome(string s) {
        int n=s.size();
        vector<int> dp(1<<10,n);
        int res=1;
        dp[0]=-1;
        int mask=0;
        for(int i=0;i<n;i++){
            char c=s[i];
            mask^=(1<<(c-'0'));
            res=max(res,i-dp[mask]);
            for(int j=0;j<10;j++){
                res=max(res,i-dp[mask^(1<<j)]);
            }
            dp[mask]=min(dp[mask],i);
        }
        return res;
    }
};
posted @ 2020-08-10 09:32  feibilun  阅读(152)  评论(0编辑  收藏  举报