0
public String minWindow(String S, String T) {
    if (T.length() > S.length())
        return "";
    HashMap<Character, Integer> set = new HashMap<>();
    for (int i = 0; i < T.length(); i++) {
        if (!set.containsKey(T.charAt(i))) {
            set.put(T.charAt(i), 1);
        } else {
            set.put(T.charAt(i), set.get(T.charAt(i)) + 1);
        }
    }
    int count = 0;
    int min = Integer.MAX_VALUE;
    int begin = 0;
    int end = 0;
    LinkedList<Integer> index = new LinkedList<>();
    HashMap<Character, Integer> record = new HashMap<>();
    for (int i = 0; i < S.length(); i++) {
        Character tmp = S.charAt(i);
        if (set.containsKey(tmp)) {
            index.add(i);
            if (record.containsKey(tmp)) {
                record.put(tmp, record.get(tmp) + 1);

            } else {
                record.put(tmp, 1);
            }

            int num1 = record.get(tmp);
            int num2 = set.get(tmp);
            if (num1 == num2) {
                count++;
            }
            if (count == set.size()) {
                Character head = S.charAt(index.peek());
                while (record.get(head) > set.get(head)) {
                    record.put(head, record.get(head) - 1);
                    index.remove();
                    head = S.charAt(index.peek());
                }
                if (index.getLast() - index.peek() < min) {
                    min = index.getLast() - index.peek();
                    begin = index.peek();
                    end = index.getLast();
                }
            }
        } else {
            continue;
        }
    }
    if (min == Integer.MAX_VALUE) {
        return "";
    } else {

        return S.substring(begin, end + 1);
    }
}

This is the my code of one Leetcode problem. But I don't think it involves the algorithm problem. So I post it here. Here it is the problem:
I use a hashmap "record" to record the duplicated characters in S and another one "set" to record the duplicated characters in T. When the number of the duplicated characters equal, plus one to variable "count";
I passed all the test except the last one S is a string of length 100000 and T is a string with length 10001.
I must use this form:

            int num1 = record.get(tmp);
            int num2 = set.get(tmp);
            if (num1 == num2) {
                count++;
            }

instead of:

            if(record.get(tmp)==set.get(tmp)){
                count++;
            }

Only like this can the two integers be compared or the "count" won't be plused. Why the first 265 test cases can pass but the last large string causes the problem? Thank you in advance.

hidemyname
  • 2,487
  • 4
  • 22
  • 39

3 Answers3

1

It is returning Integer instead of int as you have HashMap<Character, Integer>, so it is not giving expected output for ==.

You can use

if(record.get(tmp).equals(set.get(tmp))){

You can look at this (difference between an int and an Integer) as well as this (Integer equals vs. ==) answer


Why the first 265 test cases can pass but the last large string causes the problem?

Answer: The JVM is caching Integer values. == only works for numbers between -128 and 127

Community
  • 1
  • 1
Naman Gala
  • 4,480
  • 1
  • 18
  • 48
1

Because the value of your Maps are Integer. Integer are objects and have to compared using the equals method.

if(record.get(tmp).equals(set.get(tmp))){
Jens
  • 60,806
  • 15
  • 81
  • 95
  • So can I understand like this? The first 265 cases passed because of the Integer pool like string. And because of the integer in the last case is too large to be stored in the pool. So "==" became useless. – hidemyname Apr 16 '15 at 12:00
  • @deathlee Yes that's it. – Jens Apr 16 '15 at 12:01
1

an it's an "autoboxing trap", you are putting Integer in record and set. If you call the get method you get two Integer thant must be compared with equals. When you write

int num1 = record.get(tmp);

2 distinct operations happen

  1. Retrieve the Integer
  2. Convert the Integer to int (so you can use ==)

another trap is with null objects it the Integer is null

int num1 = record.get(tmp);

gives you a NullPointerException

Giovanni
  • 3,561
  • 1
  • 19
  • 25