A set and A bucket Solution


  • 1
    O

    To pass the problem I have to add an additional set:

    public class ValidWordAbbr {
        private int[][][] h = new int[26][26][1000];
        private HashSet<String> set = new HashSet();
    
        public ValidWordAbbr(String[] dictionary) {
            for (String s : dictionary) 
                if (s.length() > 0 && !set.contains(s)) {
                    h[s.charAt(0) - 'a'][s.charAt(s.length() - 1) - 'a'][s.length()]++;
                    set.add(s);
                }
        }
    
        public boolean isUnique(String s) {
            return !set.contains(s) && (s.length() < 1 || h[s.charAt(0) - 'a'][s.charAt(s.length() - 1) - 'a'][s.length()] == 0)
                 || set.contains(s) &&                    h[s.charAt(0) - 'a'][s.charAt(s.length() - 1) - 'a'][s.length()] == 1;
        }
    }

  • 0

    There are two issues with this solution:

    1. Can not handle the words longer than 1000 of length.
    2. Can only handle the words with all lower case.

Log in to reply
 

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