Hello People,
I am doing a seemingly easy Bronze problem and I believe my algorithm to be correct. However, I receive timeouts for 9 out of 15 test cases. In this particular case, I suppose the reason is that the inputs are as long as a textbook… However, if I transpose the same algorithm in CPP, will it work? I just want to know whether it was Java’s problem that caused timeouts.
G
public class Censoring_8_11 {
public static void main(String[] args) throws Exception {
//Basic and input setup
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(br.readLine());
String S = st.nextToken();
st = new StringTokenizer(br.readLine());
String T = st.nextToken();
int cycles = 1;
int leftOff = 0;
int count = 0;
for (int n = 0; n < cycles; n++) {
count = 0;
//Get the new S into testing from whatever got last time
//Check the first T within S
for (int i = leftOff; i < S.length() - T.length() + 1; i++) {
if (count == 0) { //Make sure only the first one comes into play. Count clears once every first T is found.
String partS;
if (cycles == 1) {
partS = S.substring(i, i + T.length()); //Get string partS
} else {
partS = S.substring(i, i + T.length()); //Get string partS
}
if (partS.equals(T)) { //A new string for comparison
count++;
S = S.substring(0, i) + S.substring(i + T.length(), S.length());
cycles++;
leftOff = i;
if (leftOff - T.length() > 0) {
leftOff = leftOff - T.length();
}else {
leftOff = 0;
}
}
}
}
}
pw.println(S);
pw.close();
}
}