Why you failed the last test case: An interesting bug when I used two HashMaps in Java
The logic of my code is just as most other people, to use two HashMap to count t and s. \nI encountered an interesting bug. The code is as following:\n\n\npubli
The logic of my code is just as most other people, to use two HashMap to count t and s. \nI encountered an interesting bug. The code is as following:\n```\n\npublic String minWindow(String s, String t) {\n        char[] ss = s.toCharArray();\n        char[] tt = t.toCharArray();\n        Map<Character, Integer> map_s = new HashMap<>();\n        Map<Character, Integer> map_t = new HashMap<>();\n        int end = 0;\n        for (int i = 0; i < tt.length; i++) {\n            char c = tt[i];\n            map_t.put(c, map_t.getOrDefault(c, 0) + 1);\n            if (!map_s.containsKey(c) || map_s.get(c) < map_t.get(c)) { \n                while(end < ss.length && ss[end] != c) {\n                    map_s.put(ss[end], map_s.getOrDefault(ss[end], 0) + 1);\n                    end++;\n                }\n                if (end == ss.length) return "";\n                map_s.put(c, map_t.get(c));\n                end++;\n            }\n        }\n        int min = end;\n        int st = 0;\n        for (int i = 0; i < end; i++) {\n            char c = ss[i];\n            if (c == \'m\') {\n                if (!map_s.containsKey(c) || !map_t.containsKey(c)) continue;\n                if (map_s.get(c) == 423 && map_t.get(c) == 423 && map_s.get(c) != map_t.get(c)) {\n                    System.out.println("What the hell is going on here?");\n                }\n            }\n            if (map_t.containsKey(c)) {\n                if (map_s.get(c) == map_t.get(c)) {\n                    if (end - i < min) {\n                        st = i;\n                        min = end - i;\n                    }\n                    while(end < ss.length && ss[end] != c) {\n                        if (map_t.containsKey(ss[end])) map_s.put(ss[end], map_s.get(ss[end]) + 1);\n                        end++;\n                    }\n                    if (end == ss.length) break;\n                    end++;\n                } else {\n                    map_s.put(c, map_s.get(c) - 1);\n                }\n            }\n        }\n        return s.substring(st, st + min);\n    }\n```\n\nIf you copy paste my code, you\'ll see it passes 267/268 but fails at the last test case. And you\'ll see in the last test case, \nthe code will outpu :"What the hell is going on here?".\n\nThe algorithm logic of my code is 100% correct. But something wrong happened in the implementation. As you see, when map_s.get(c) == 423 &&  map_t.get(c) == 423, actuall map_s.get(c) != map_t.get(c). Interesting and annoying(it took me a few hours to discover this bug T_T), but why?\n\nThe reason is when you put into maps some int value as Integer, the autoboxing process calls the \'valueOf()\' method. The \'valueOf()\' function would return the reference of the object from Integer cache if the value is in the range of -128~127. However, like in the above test case, the two values are 423, then \'valueOf()\' function return two different references when we put 423 into the two maps. In this case, the two Integers, both has value of 423, are two different objects, and they are not == to each other! \n\nSo guys we should really be careful when compare two Integer objects instead of two int!!!
[]