Super easy java solution runtime beats 95.27 % of java submissions.


  • 0
    C
    class MapSum {
    
        Map<String,Integer> map;
    	
    	/** Initialize your data structure here. */
        public MapSum() {
        	map = new HashMap<String,Integer>();
        }
        
        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;
        }
    }
    

Log in to reply
 

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