Created
March 5, 2016 01:45
-
-
Save jianminchen/c6b51207f9cc9b083573 to your computer and use it in GitHub Desktop.
HackerRank Bear And Steady Gene
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <cstdio> | |
#include <vector> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int n,ans,kiri,kanan,i; | |
int a[1007]; | |
char s[1000007]; | |
bool valid() { | |
int A,G,T,C; | |
A = a['A']; | |
G = a['G']; | |
T = a['T']; | |
C = a['C']; | |
if (A <= n/4 && G <= n/4 && T <= n/4 && C <= n/4) return true; | |
return false; | |
} | |
int main() { | |
scanf("%d",&n); | |
scanf("%s",&s); | |
for (i=0 ; i<n ; i++) { | |
a[s[i]]++; | |
} | |
if (valid()) { | |
printf("0\n"); | |
return 0; | |
} | |
ans = 999999999; | |
kiri = 0; | |
for (kanan=0 ; kanan<n ; kanan++) { | |
a[s[kanan]]--; | |
while(valid() && kiri <= kanan) { | |
ans = min(ans,kanan-kiri+1); | |
a[s[kiri]]++; | |
kiri++; | |
} | |
} | |
printf("%d\n",ans); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment