Java-Map solution!


  • 2
    T
    class MapSum {
        Map<String,Integer> map = new HashMap<>();
        /** Initialize your data structure here. */
        public MapSum() {
            
        }
        
        public void insert(String key, int val) {
               map.put(key,val);
        }
        
        public int sum(String prefix) {
            int sum = 0;
            for(String str:map.keySet()){
                if(str.startsWith(prefix)){
                    sum+=map.get(str);
                }
            }
            
            return sum;
        }
    }
    
    /**
     * Your MapSum object will be instantiated and called as such:
     * MapSum obj = new MapSum();
     * obj.insert(key,val);
     * int param_2 = obj.sum(prefix);
     */
    
    

  • 0
    T

    re-write my solutions using Trie Tree:
    TrieTree should be the best solution!

    class MapSum {
        class TrieNode{
            Map<Character,TrieNode> childMap = new HashMap<>();
            int sum;
            TrieNode(){
                
            }
        }
        TrieNode root;
        Map<String,Integer> map = new HashMap<>();
        /** Initialize your data structure here. */
        public MapSum() {
             root = new TrieNode();
        }
        
        public void insert(String key, int val) {
            int delta = val - map.getOrDefault(key,0);
            map.put(key,val);
            
            TrieNode current = root;
            char[] array = key.toCharArray();
            root.sum+=delta;
            for(int i=0;i<array.length;i++){
                char c = array[i];
                if(current.childMap.containsKey(c)==false){
                    current.childMap.put(c,new TrieNode());
                }
                
                current = current.childMap.get(c); 
                current.sum+=delta;
            }
        }
        
        public int sum(String prefix) {
            char[] array = prefix.toCharArray();
            TrieNode current = root;
            for(int i=0;i<array.length;i++){
                char c = array[i];
                if(current.childMap.containsKey(c)==false){
                    return 0;
                }
                current = current.childMap.get(c);
            }
            
            return current.sum;
        }
    }
    
    /**
     * Your MapSum object will be instantiated and called as such:
     * MapSum obj = new MapSum();
     * obj.insert(key,val);
     * int param_2 = obj.sum(prefix);
     */
    
    

  • 2
    T

    I got pretty much similar solution

    class MapSum {
        private Map<String,Integer> map = new HashMap<>();
        /** Initialize your data structure here. */
        public MapSum() {
            
        }
        
        public void insert(String key, int val) {
            map.put(key,val);
        }
        
        public int sum(String prefix) {
            int sum=0;
            for(String key : map.keySet()){
                if(key.indexOf(prefix) == 0){
                    sum+=map.get(key);
                }
            }
            return sum;
        }
    }
    
    

Log in to reply
 

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