Simple Brute Force Java Solution using little bit of lambda. AC

  • 0
    public class MapSum {
       * Initialize your data structure here.
      private Map<String, Integer> mapSum;
      public MapSum() {
        mapSum = new HashMap<>();
      public void insert(String key, int val) {
        assert key != null;
        mapSum.put(key, val);
      public int sum(String prefix) {
        int total = mapSum.keySet().stream().filter(s -> s.startsWith(prefix))
            .mapToInt(s -> mapSum.get(s)).sum();
        return total;

Log in to reply

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