Skip to content

Instantly share code, notes, and snippets.

@kamal-github
Created April 10, 2016 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamal-github/3a136d09c4a2074aeb08ea6a741edc74 to your computer and use it in GitHub Desktop.
Save kamal-github/3a136d09c4a2074aeb08ea6a741edc74 to your computer and use it in GitHub Desktop.
Bear and Steady Gene (CPP program)
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){
break;
}
int substringLength = max(0, maxIndex - minIndex - 1);
if(substringLength < out){
out = substringLength;
}
dict[str[minIndex + 1]]++;
}
cout << out << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment