Java neat but slow solution


  • 0
    J
    class MapSum {
    
        Map<String, Integer> map =new HashMap<>();
        
        public void insert(String key, int val) {
            map.put(key, val);
        }
        
        public int sum(String prefix) {
            return map.entrySet().stream().filter(map -> map.getKey().startsWith(prefix)==true).mapToInt(map->map.getValue()).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);
     */
    

Log in to reply
 

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