Created
March 3, 2016 05:51
-
-
Save NeoZhangTCL/e1d5b6b9ee80f5c9b749 to your computer and use it in GitHub Desktop.
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
import java.io.*; | |
import java.util.*; | |
public class Solution { | |
public static void main(String[] args) throws FileNotFoundException { | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ | |
//uncomment it when doing in Eclipse | |
//System.setIn(new FileInputStream(args[0])); | |
Scanner in = new Scanner(System.in); | |
int length = in.nextInt(); | |
int maxEach = length/4; | |
in.nextLine(); | |
String gene = in.nextLine(); | |
int counterG = 0, counterA=0, counterC=0, counterT = 0; | |
Stack<Character> left = new Stack<Character>(); | |
Stack<Character> right = new Stack<Character>(); | |
List<Integer> lengths = new ArrayList<Integer>(); | |
int i; | |
for (i=0; i<length; i++){ | |
if (gene.charAt(i)=='G'){ | |
counterG++; | |
left.add('G'); | |
} | |
if (gene.charAt(i)=='A'){ | |
counterA++; | |
left.add('A'); | |
} | |
if (gene.charAt(i)=='C'){ | |
counterC++; | |
left.add('C'); | |
} | |
if (gene.charAt(i)=='T'){ | |
counterT++; | |
left.add('T'); | |
} | |
if (counterG > maxEach|| counterA > maxEach|| counterC > maxEach|| counterT > maxEach){ | |
lengths.add(left.size()-1); | |
char removed = left.pop(); | |
if (removed=='G'){ | |
counterG--; | |
} | |
if (removed=='A'){ | |
counterA--; | |
} | |
if (removed=='C'){ | |
counterC--; | |
} | |
if (removed=='T'){ | |
counterT--; | |
} | |
break; | |
} | |
} | |
int j=length-1; | |
while(j>0 && !left.isEmpty()){ | |
if (gene.charAt(j)=='G'){ | |
counterG++; | |
right.add('G'); | |
} | |
if (gene.charAt(j)=='A'){ | |
counterA++; | |
right.add('A'); | |
} | |
if (gene.charAt(j)=='C'){ | |
counterC++; | |
right.add('C'); | |
} | |
if (gene.charAt(j)=='T'){ | |
counterT++; | |
right.add('T'); | |
} | |
while((counterG > maxEach|| counterA > maxEach|| counterC > maxEach|| counterT > maxEach)&&!left.isEmpty()){ | |
int len = left.size() + right.size() - 1; | |
lengths.add(len); | |
char removed = left.pop(); | |
if (removed=='G'){ | |
counterG--; | |
} | |
if (removed=='A'){ | |
counterA--; | |
} | |
if (removed=='C'){ | |
counterC--; | |
} | |
if (removed=='T'){ | |
counterT--; | |
} | |
} | |
j--; | |
} | |
int result = length - Collections.max(lengths); | |
System.out.println(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment