We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A simple greedy solution. Works at O(N). Please comment if any better solution or anything to improve. It can be shorter than this, but it will affect readability.
bool validityCheck(map<char, int> dict, int limit){
if(dict['A'] <= limit && dict['C'] <= limit && dict['G'] <= limit && dict['T'] <= limit){
return true;
}else{
return false;
}
}
int main() {
int N, maxIndex = 0, out = 999999;
map<char, int> dict;
string str;
cin >> N >> str;
int limit = N / 4;
for (int i = N - 1; i >= 0; i--){
dict[str[i]]++;
if(!validityCheck(dict, limit)){
maxIndex = i + 1;
dict[str[i]]--;
break;
}
}
for(int minIndex = -1; minIndex < N - 1 && maxIndex < N && minIndex < maxIndex; minIndex++){
while(!validityCheck(dict, limit) && maxIndex < N){
dict[str[maxIndex]]--;
maxIndex++;
}
if(maxIndex > N || !validityCheck(dict, limit)){
break;
}
int substringLength = max(0, maxIndex - minIndex - 1);
if(substringLength < out){
out = substringLength;
}
dict[str[minIndex + 1]]++;
}
cout << out << endl;
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bear and Steady Gene
You are viewing a single comment's thread. Return to all comments →
A simple greedy solution. Works at O(N). Please comment if any better solution or anything to improve. It can be shorter than this, but it will affect readability.