Please add one more test case


  • 1

    I misunderstood the description but still got AC.
    Please add this test case:
    ["MapSum", "insert", "insert", "insert", "sum"]
    [[], ["apple",3], ["ap",4], ["apple",5], ["ap"]]

        Map<String, Integer> valueMap;
        Map<String, Integer> prefixMap;
        /** Initialize your data structure here. */
        public MapSum() {
            valueMap = new HashMap<>();
            prefixMap = new HashMap<>();
        }
        public void insert(String key, int val) {
            StringBuilder temp = new StringBuilder();
            for (int i = 0; i < key.length(); i++) {
                temp.append(key.charAt(i));
                prefixMap.put(temp.toString(), valueMap.containsKey(key)? val : 
                                       prefixMap.getOrDefault(temp.toString(), 0) + val);
            }
            valueMap.put(key, val);
        }  
        public int sum(String prefix) {
            return prefixMap.getOrDefault(prefix, 0);
        }
    

Log in to reply
 

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