Java Solution


  • 1
    S
        public String findLongestWord(String s, List<String> d) {
            String longest = "";
            for(String word:d){
                if(word.length()>=longest.length()){
                    int curI = 0;
                    for(int i =0; i<s.length() && curI<word.length(); i++){
                        char c = s.charAt(i);
                        if(c==word.charAt(curI)){
                            curI++;
                        }
                    }
                    if(curI==word.length()){
                        if(word.length()==longest.length()&&word.compareTo(longest)>=0){
                            continue;
                        }
                        longest = word;
                    }
                }
            }
            return longest;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.