Codility/Lesson 5/Prefix Sums/GenomicRangeQuery/Java


Code
// you can also use imports, for example:
import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int[] solution(String S, int[] P, int[] Q) {
        // write your code in Java SE 8
        int A_changeData[] = new int[S.length() + 1];
        int C_changeData[] = new int[S.length() + 1];
        int G_changeData[] = new int[S.length() + 1];
        int T_changeData[] = new int[S.length() + 1];

        for(int i=0; i<S.length(); i++) {
            char currentChar = S.charAt(i);

            if(currentChar == 'A') {
                A_changeData[i+1] = A_changeData[i] + 1;
                C_changeData[i+1] = C_changeData[i];
                G_changeData[i+1] = G_changeData[i];
                T_changeData[i+1] = T_changeData[i];
            } else if(currentChar == 'C') {
                A_changeData[i+1] = A_changeData[i];
                C_changeData[i+1] = C_changeData[i] + 1;
                G_changeData[i+1] = G_changeData[i];
                T_changeData[i+1] = T_changeData[i];
            } else if(currentChar == 'G') {
                A_changeData[i+1] = A_changeData[i];
                C_changeData[i+1] = C_changeData[i];
                G_changeData[i+1] = G_changeData[i] + 1;
                T_changeData[i+1] = T_changeData[i];
            } else {
                A_changeData[i+1] = A_changeData[i];
                C_changeData[i+1] = C_changeData[i];
                G_changeData[i+1] = G_changeData[i];
                T_changeData[i+1] = T_changeData[i] + 1;
            }
        }

        int answer[] = new int[P.length];
        for(int i=0; i<P.length; i++) {
            int from = P[i];
            int to = Q[i];

            if(A_changeData[from] != A_changeData[to+1]) {
                answer[i] = 1;
            } else if(C_changeData[from] != C_changeData[to+1]) {
                answer[i] = 2;
            } else if(G_changeData[from] != G_changeData[to+1]) {
                answer[i] =3;
            } else{
                answer[i] = 4;
            }
        }

        return answer;
    }
}
Result URL
https://app.codility.com/demo/results/trainingRKYHK3-W7W/