You are viewing a single comment's thread. Return to all comments →
Fastest Solution in C++
using namespace std;
int main(){ ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n; cin.ignore(); string s; cin >> s; vector<int> freq(26); for(char c : s) ++freq[c - 'a']; int max_len = 0; for(char c1 = 'a'; c1 <= 'z'; ++c1) { if(freq[c1 - 'a'] == 0) continue; for(char c2 = c1 + 1; c2 <= 'z'; ++c2) { if(freq[c2 - 'a'] == 0) continue; if(abs(freq[c1 - 'a'] - freq[c2 - 'a']) > 1) continue; if(freq[c1 - 'a'] + freq[c2 - 'a'] < max_len) continue; char last = '\0'; int len = 0; for(char c : s) { if(c == last) { len = 0; break; } if(c != c1 && c != c2) continue; last = c; ++len; } max_len = max(max_len, len); } } cout << max_len << endl; return 0;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
Fastest Solution in C++
include
include
using namespace std;
int main(){ ios::sync_with_stdio(false); cin.tie(nullptr);
}