package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/longest-word-in-dictionary/
 */
public class Problem720 {
    public String longestWord(String[] words) {
        Arrays.sort(words, (a, b) -> a.compareTo(b));
        String tmp = words[0];
        List<WordCount> result = new ArrayList<>();
        int count = 1;
        for (int i = 1; i < words.length; i++) {
            if (tmp.length() + 1 == words[i].length() && words[i].startsWith(tmp)) {
                tmp = words[i];
                count++;
            } else {
                result.add(new WordCount(tmp, count));
                tmp = words[i];
                count = 1;
            }
        }
        result.add(new WordCount(tmp, count));
        result.sort((a, b) -> {
            if (a.count < b.count) {
                return 1;
            } else if (a.count > b.count) {
                return -1;
            } else {
                return a.word.compareTo(b.word);
            }
        });
        return result.get(0).word;
    }

    private static class WordCount {
        private String word;
        private int count;

        public WordCount(String word, int count) {
            this.word = word;
            this.count = count;
        }

        @Override
        public String toString() {
            return word + ": " + count;
        }
    }

    public static void main(String[] args) {
        Problem720 prob = new Problem720();
//        System.out.println(prob.longestWord(new String[]{"w","wo","wor","worl", "world"})); // world
//        System.out.println(prob.longestWord(new String[]{"a", "banana", "app", "appl", "ap", "apply", "apple"})); // apple
//        System.out.println(prob.longestWord(new String[]{"a"})); // a
//        System.out.println(prob.longestWord(new String[]{"a", "b"})); // a
//        System.out.println(prob.longestWord(new String[]{"m", "mo", "moc", "moch", "mocha", "l", "la", "lat", "latt", "latte", "c", "ca", "cat"})); // latte
        System.out.println(prob.longestWord(new String[]{"ogz","eyj","e","ey","hmn","v","hm","ogznkb","ogzn","hmnm","eyjuo","vuq","ogznk","og","eyjuoi","d"})); // eyj
    }
}

@@ -311,16 +311,10 @@
 p = 
-words%5B0%5D
+%22%22
 ;%0A  
@@ -324,25 +324,22 @@
    List%3C
-WordCount
+String
 %3E result
@@ -364,31 +364,8 @@
 ();%0A
-        int count = 1;%0A
     
@@ -381,17 +381,17 @@
 int i = 
-1
+0
 ; i %3C wo
@@ -429,108 +429,126 @@
 if (
-tmp.length() + 1 == words%5Bi%5D.length() && words%5Bi%5D.startsWith(tmp)) %7B%0A                tmp = words%5Bi%5D;
+words%5Bi%5D.length() == 1) %7B%0A                if (!tmp.isEmpty()) %7B%0A                    result.add(tmp);%0A                %7D
 %0A   
@@ -564,15 +564,22 @@
     
-count++
+tmp = words%5Bi%5D
 ;%0A  
@@ -617,47 +617,85 @@
     
-result.add(new WordCount(tmp, count));%0A
+if (tmp.length() + 1 == words%5Bi%5D.length() && words%5Bi%5D.startsWith(tmp)) %7B%0A    
     
@@ -742,18 +742,9 @@
     
-count = 1;
+%7D
 %0A   
@@ -787,33 +787,11 @@
 add(
-new WordCount(tmp, count)
+tmp
 );%0A 
@@ -843,23 +843,29 @@
  (a.
-count %3C b.count
+length() %3C b.length()
 ) %7B%0A
@@ -919,23 +919,29 @@
  (a.
-count %3E b.count
+length() %3E b.length()
 ) %7B%0A
@@ -1017,13 +1017,8 @@
 n a.
-word.
 comp
@@ -1024,21 +1024,16 @@
 pareTo(b
-.word
 );%0A     
@@ -1085,342 +1085,9 @@
 t(0)
-.word;%0A    %7D%0A%0A    private static class WordCount %7B%0A        private String word;%0A        private int count;%0A%0A        public WordCount(String word, int count) %7B%0A            this.word = word;%0A            this.count = count;%0A        %7D%0A%0A        @Override%0A        public String toString() %7B%0A            return word + %22: %22 + count;%0A        %7D
+;
 %0A   
@@ -1715,16 +1715,18 @@
 / latte%0A
+//
         
@@ -1884,16 +1884,315 @@
  // eyj%0A
+//        System.out.println(prob.longestWord(new String%5B%5D%7B%22yo%22,%22ew%22,%22fc%22,%22zrc%22,%22yodn%22,%22fcm%22,%22qm%22,%22qmo%22,%22fcmz%22,%22z%22,%22ewq%22,%22yod%22,%22ewqz%22,%22y%22%7D)); // yodn%0A        System.out.println(prob.longestWord(new String%5B%5D%7B%22rac%22,%22rs%22,%22ra%22,%22on%22,%22r%22,%22otif%22,%22o%22,%22onpdu%22,%22rsf%22,%22rs%22,%22ot%22,%22oti%22,%22racy%22,%22onpd%22%7D)); // otif%0A
     %7D%0A%7D%0A

Update problem 720