Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 5, 2016 09:24
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 jianminchen/fae9142eff9a6f9643fc to your computer and use it in GitHub Desktop.
Save jianminchen/fae9142eff9a6f9643fc to your computer and use it in GitHub Desktop.
Bear And Steady Gene - readable code
using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int N = Int32.Parse(Console.ReadLine());
int A = 0;
int C = 0;
int G = 0;
int T = 0;
string line = Console.ReadLine();
for(int i=0; i<N; i++) {
switch(line[i]) {
case 'A': A++; break;
case 'C': C++; break;
case 'G': G++; break;
case 'T': T++; break;
}
}
A -= N/4; C -= N/4; G-= N/4; T -= N/4;
if(A == 0 && C == 0 && G == 0 && T == 0) {
Console.WriteLine("0");
} else if(A+C+G+T == 1) {
Console.WriteLine("1");
} else {
int min = N;
int best = Math.Max(0,A) + Math.Max(0,C) + Math.Max(0,G) + Math.Max(0,T);
int start = 0;
int end = 0;
switch(line[0]) {
case 'A': A--; break;
case 'C': C--; break;
case 'G': G--; break;
case 'T': T--; break;
}
while(min != best && start<N && end<N) {
if(A <= 0 && C <=0 && G <= 0 && T <= 0) {
if(end-start+1 < min)
min = end-start+1;
switch(line[start]) {
case 'A': A++; break;
case 'C': C++; break;
case 'G': G++; break;
case 'T': T++; break;
}
start++;
} else {
end++;
if(end != N) {
switch(line[end]) {
case 'A': A--; break;
case 'C': C--; break;
case 'G': G--; break;
case 'T': T--; break;
}
}
}
}
Console.WriteLine(min);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment