Can anyone help why this code gives a runtime error in kickstart while sample test cases working properly.
I have tried the same logic in c++ and it got accepted.
import java.io.*;
import java.util.*;
public class Solution {
static PrintWriter w = new PrintWriter(System.out);
static Reader sc = new Reader();
public static void main(String args[]) throws IOException {
int tc = 1;
tc = sc.ni();
for (int i = 1; i <= tc; i++) {
w.print("Case #" + (i) + ": ");
solve();
}
w.close();
}
public static void solve() {
String s = sc.next();
int countF = 0;
int ans = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
if (s.charAt(i) == 'K' && len >= i + 3) {
if (s.charAt(i + 1) == 'I' && s.charAt(i + 2) == 'C' && s.charAt(i + 3) == 'K') {
countF++;
i += 2;
continue;
}
} else if (s.charAt(i) == 'S' && len >= i + 4) {
if (s.charAt(i + 1) == 'T' && s.charAt(i + 2) == 'A' && s.charAt(i + 3) == 'R'
&& s.charAt(i + 4) == 'T') {
ans += countF;
i += 3;
}
}
}
w.println(ans);
}
static class Reader {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public double nd() {
return Double.parseDouble(next());
}
public String nline() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}