leetcode 耗时100 1763. Longest Nice Substring Problem: 1763. 最长的美好子字符串这道题真不简单有点难分治算法的哈希表耗时100%分别统计大写小写小写1大写1000然后分治的若当前字符是小写且 (tr[s[i] - ‘a’] 100 || tr[s[i] - ‘a’] % 1000 0))或者大写且(tr[s[i] - ‘A’] 100 || tr[s[i] - ‘A’] % 1000 0))也就是这个字符串内只出现了大写或者小写分治字串判断是否满足条件若 pre 0就满足条件返回拿到长度最长的字串返回Codeclass Solution { public: string longestNiceSubstring(string s) { int n s.size(); if(n 1) return ; vectorint tr(26, 0); for(char c : s) { if(islower(c)) tr[c - a] 1; else if(isupper(c)) tr[c - A] 1000; } int pre 0; string tg, now; for(int i 0; i n; i) { if((islower(s[i]) (tr[s[i] - a] 100 || tr[s[i] - a] % 1000 0)) || (isupper(s[i]) (tr[s[i] - A] 100 || tr[s[i] - A] % 1000 0)) ) { if(i-pre 1) { tg longestNiceSubstring(s.substr(pre, i - pre)); if(tg.size() now.size()) { now tg; } } pre i 1; } } if(pre 0) return s; if(n - pre 0 n - pre 1) { tg longestNiceSubstring(s.substr(pre, n - pre)); if(tg.size() now.size()) { now tg; } } return now; } };