Posted on

Description

Submission

class Solution {
    bool checkOK(const string& s) {
        unordered_set<char> Set{s.begin(), s.end()};

        for(char ch: s) {
            char target;
            if(ch <= 'z' && ch >= 'a') target = ch - 32;
            else if(ch <= 'Z' && ch >= 'A') target = ch + 32;
            if(!Set.count(target)) return false;
        }

        return true;
    }
public:
    string longestNiceSubstring(string s) {
        int n = s.length();
        unordered_map<char, int> Map;

        string ret;
        for(int i = 0; i < n; ++i) {
            for(int len = ret.length(); len < n - i + 1; ++len) {
                if(len <= ret.length()) continue;
                string sub = s.substr(i, len);
                if(checkOK(sub)) ret = sub;
            }
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *