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.
public static int steadyGene(String gene) {
int a =0, c=0, g=0, t=0;
for(int i=0; i<gene.length(); i++)
{
if(gene.charAt(i)=='A')a+=1;
else if(gene.charAt(i)=='C')c+=1;
else if(gene.charAt(i)=='G')g+=1;
else t+=1;
}
int n = gene.length()/4;
int A =0, C = 0, G = 0, T = 0, tot = 0;
if(a>n)
{
A = a-n;tot+=A;
}
if(c>n)
{
C = c-n;tot+=C;
}
if(g>n)
{
G = g-n;tot+=G;
}
if(t>n)
{
T = t-n;tot+=T;
}
while(true)
{
String s= gene.substring(0, tot+1);
a=0;c=0;g=0;t=0;
for(int i=0; i<tot; i++)
{
if(s.charAt(i)=='A')a+=1;
else if(s.charAt(i)=='C')c+=1;
else if(s.charAt(i)=='G')g+=1;
else t+=1;
}
for(int i=0; i<gene.length()-tot; i++)
{
if(i>0)
{
if(gene.charAt(i-1)=='A')a-=1;
else if(gene.charAt(i-1)=='C')c-=1;
else if(gene.charAt(i-1)=='G')g-=1;
else t-=1;
if(gene.charAt(i+tot-1)=='A')a+=1;
else if(gene.charAt(i+tot-1)=='C')c+=1;
else if(gene.charAt(i+tot-1)=='G')g+=1;
else t+=1;
}
if(a>=A && c>=C && g>=G && t>=T)
{
return tot;
}
}
tot++;
if(tot>gene.length())
break;
}
return 0;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
String gene = bufferedReader.readLine();
int result = Result.steadyGene(gene);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
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 →
import java.io.; import java.math.; import java.security.; import java.text.; import java.util.; import java.util.concurrent.; import java.util.function.; import java.util.regex.; import java.util.stream.*; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList;
class Result {
}
public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
}