@Weiyu:
Same problem for me. I passed the visible test, but failed the invisible test 
with the following code.

(even Upcasing all words in-before and changing the List to a Set does not 
improve the result)

Which edge-case did I forget?

------
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import javax.xml.stream.events.Characters;

public class Solution {
        public static void main(String[] args) {
                Scanner in = new Scanner(new BufferedReader(new 
InputStreamReader(System.in)));
                int t = in.nextInt(); // Scanner has functions to read ints, 
longs, strings, chars, etc.
                for (int i = 1; i <= t; i++) {
                        int n = in.nextInt();
                        int l = in.nextInt();
                        List<String> words = new LinkedList<String>();
                        List<Set<Character>> letters = new 
ArrayList<Set<Character>> ();
                        //initialize LetterList
                        for (int x=0; x<l; x++) {
                                HashSet<Character> set = new 
HashSet<Character>();
                                letters.add(set);
                        }
                        // screen words
                        for (int j=0; j<n; j++) {
                                String word = in.next();
                                words.add(word);
                                for (int y=0;y<l;y++) {
                                        // check, if letters of word are 
already part of it
                                        Character letter = word.charAt(y);
                                        Set<Character> set = letters.get(y);
                                        set.add(letter);
                                        letters.set(y, set);
                                }
                        }
                        
                        // check, if no word exists
                        int totalWords=1;
                        for (int z=0;z<l;z++) {
                                Set<Character> set = letters.get(z);
                                totalWords *= set.size();
                        }
                        if (words.size() == totalWords) {
                                System.out.println("Case #" + i + ": -");
                        } else {
                        
                                // a word exists, now we have to find it
                                String finalWord = "";
                                
                                for (int a=0; a<l; a++) {
                                        //determine a'th Letter of Word
                                        for (Character currentLetter: 
letters.get(a)) {
                                                // let's suppose that the 
letter at a would be currentLetter
                                                List<String> wordsWithCharacter 
= new LinkedList<String>();
                                                //determine words that have 
this character at a
                                                for (String word: words) {
                                                        if (word.charAt(a) == 
currentLetter.charValue()) {
                                                                
wordsWithCharacter.add(word);
                                                        }
                                                }
                                                // check, if there are words 
left with current Character at a
                                                int totalWordsWithCharacter = 1;
                                                for (int b=a+1; b<l; b++) {
                                                        Set<Character> set = 
letters.get(b);
                                                        totalWordsWithCharacter 
*= set.size();
                                                }
                                                if 
(wordsWithCharacter.size()<totalWordsWithCharacter) {
                                                        // then current Letter 
is definitively an Option
                                                        words = 
wordsWithCharacter; // eases further computation
                                                        finalWord = 
finalWord+currentLetter;
                                                        break; // go to next 
letter
                                                } else {
                                                        // current letter is no 
option
                                                        // check next letter
                                                }
                                        }
                                        // check letter for next position
                                }
                                
                                System.out.println("Case #" + i + ": " + 
finalWord);
                        }
                }
        }
}

-- 
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/16da474e-3771-4462-9b59-b64577ac92a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to