24 ms beat 100% But you need to make some assumptions


  • 0
    Q

    Assume the word length will not be more than 30

    public class ValidWordAbbr {
        String[][][] map;
        public ValidWordAbbr(String[] dictionary) {
            map = new String[26][26][30];
            for (String s : dictionary) {
                if (s.length() < 3) {
                    continue;
                }
                int p1 = s.charAt(0);
                int p2 = s.charAt(s.length()-1);
                int p3 = s.length();
                if (map[p1-'a'][p2-'a'][p3] == null) {
                    map[p1-'a'][p2-'a'][p3] = s;
                }
                else {
                    map[p1-'a'][p2-'a'][p3] = "";
                }
            }
        }
    
        public boolean isUnique(String word) {
            if (word.length() < 3) {
                return true;
            }
            int p1 = word.charAt(0);
            int p2 = word.charAt(word.length()-1);
            int p3 = word.length();
            if (map[p1-'a'][p2-'a'][p3] == null){
                return true;
            }
            else {
                if (map[p1-'a'][p2-'a'][p3].equals(word)) {
                    return true;
                }
                return false;
            }
        }
    }

Log in to reply
 

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