Share my AC solution


  • 0
    S
    public String findLongestWord(String s, List<String> d) {
    	int max = 0;
    	String res = "";
    	for (String str : d) {
    		int id = -1;
    		boolean find = true;
    		for (char c : str.toCharArray()) {
    			id = s.indexOf(c, id+1);
    			if (id == -1) {
    				find = false;
    				break;
    			}
    		}
    		if (find){
    			if (str.length() > max){
    				max = str.length();
    				res = str;
    			}else if (str.length() == max) {
    				for (int i = 0; i < res.length(); i++) {
    					if (str.charAt(i) == res.charAt(i)) continue;
    					if (str.charAt(i) < res.charAt(i)) res = str;
    					break;
    				}
    			}
    		}
    	}
    	return res;
    }
    

Log in to reply
 

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