easy to understand O(n*k)


  • 0
    B
    public class Solution {
        public String findLongestWord(String s, List<String> d) {
            String longest = "";
            for (String word : d) {
                if (word.length() < longest.length()) {
                    continue;
                }
                int i = 0;
                int j = 0;
                while (j < word.length() && i < s.length()) {
                    if (word.charAt(j) == s.charAt(i)) {
                        j++;
                        i++;
                    } else {
                        i++;
                    }
                }
                if (j == word.length() && (word.length() > longest.length() || (word.length() == longest.length() && word.compareTo(longest) < 0))) {
                    longest = word;
                }
            }
            
            return longest;
        }
    }

Log in to reply
 

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