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.
/*
* Complete the 'steadyGene' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING gene as parameter.
*/
public static int steadyGene(String gene) {
int n = gene.length();
int target = n / 4;
int[] count = new int[128];
for (char c : gene.toCharArray()) {
count[c]++;
}
// If already steady
if (count['A'] == target && count['C'] == target && count['G'] == target && count['T'] == target) {
return 0;
}
int minLen = n;
int left = 0;
for (int right = 0; right < n; right++) {
count[gene.charAt(right)]--;
while (count['A'] <= target && count['C'] <= target && count['G'] <= target && count['T'] <= target) {
minLen = Math.min(minLen, right - left + 1);
count[gene.charAt(left)]++;
left++;
}
}
return minLen;
}
}
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.*;
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")));
}