This code runs perfectly on my pc but received runtime error on the google
servers with their input...... anyone knows why?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(new BufferedReader(new
InputStreamReader(System.in)));
int testCasesNum = Integer.parseInt(scanner.nextLine().trim());
for (int i = 0; i < testCasesNum; i++) {
String secondRow = scanner.nextLine().trim();
String[] split = secondRow.split(" ");
int n = Integer.parseInt(split[0].trim());
int l = Integer.parseInt(split[1].trim());
String[] words = new String[n];
for (int j = 0; j < n; j++) {
words[j] = scanner.nextLine().trim();
}
String newWord = solve(words, l);
System.out.println("Case #" + (i + 1) + ": " + (newWord
== null ? "-" : newWord));
}
scanner.close();
}
private static String solve(String[] words, int l) {
if (l <= 1 || words.length == 1)
return null;
Set<String> wordsSet = new HashSet<>(words.length);
for (int i = 0; i < words.length; i++) {
wordsSet.add(words[i]);
}
double count = Math.pow(words.length, l);
for (int i = 0; i < count; i++) {
char[] newWord = new char[l];
String baseCounter = Integer.toString(i, words.length);
if (baseCounter.length() < l) {
int tempCount = l - baseCounter.length();
for (int j = 0; j < tempCount; j++) {
baseCounter = "0" + baseCounter;
}
}
char[] indecis = baseCounter.toCharArray();
for (int j = indecis.length - 1; j >=0 ; j--) {
newWord[j] = words[indecis[j] - '0'].charAt(j);
}
String newWordStr = new String(newWord);
if (!wordsSet.contains(newWordStr))
return newWordStr;
}
return null;
}
}
--
You received this message because you are subscribed to the Google Groups
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/96e9dac8-990b-4684-995e-b2b4cfeec540%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.