Search code examples
javahashmap

HashMap not replacing values


What i want to do is check if a 10 character long substring of my input repeats inside the input and I need to add it to a List if it does. This List can not have duplicates. For that purpose I'm using a HashMap. Here's how my program's logic goes:

Check if the substring is in my HashMap. If it isn't then I'd like to add the mapping (substring, 1) to my map. If it is and if the mapping = 1, then i'd like to add it to my ls ArrayList and to increment it's mapping by 1.

Curiously, my code below does not seem to be replacing the mapping to the incremeted integer(See the println and the program stdout below). I know i could solve this problem with a HashSet, but what i want to know is why HashMap is behaving so weirdly(I want to learn how it works) and how could I solve this problem using a HashMap. See input, code and output below:

Input: "AAAAAAAAAAAAA"

Code:

class Solution {
public List<String> findRepeatedDnaSequences(String s) {
    int begin_window=0;
    List<String> ls = new ArrayList<String>();
    HashMap<String,Integer> hm = new HashMap<String,Integer>();
    while(begin_window+10<=s.length()){
        String temp = s.substring(begin_window,begin_window+10);
        if( hm.put(temp,1) != null){
            System.out.println(hm.get(temp));
            if( hm.get(temp) == 1){
                ls.add(temp);
                hm.put(temp,2);
            }
        }
        begin_window++;
    }
    return ls;
}

}

Output:

["AAAAAAAAAA","AAAAAAAAAA","AAAAAAAAAA"]

The output should be ["AAAAAAAAAA"] as we don't want duplicates

Stdout:
1
1
1

For those of you who are curious, here's where the problem comes from: https://leetcode.com/problems/repeated-dna-sequences/

You can use the link above to test my code.

Thanks in advance!


Solution

  • As I can't comment because of too little reputation:

    The HashMap.put() method doesn't return null if a Key exists already. It doesn't have any return value as it is a void anyway.

    If a Key exists already in the HashMap, and you call the .put() method on it, it just overwrites the associated value.

    In your case have to check if the key exists already in the Map with HashMap.containsKey(). This returns true or false.

    You can check it in an if condition and do your increment and everything else:

    class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
        int begin_window=0;
        List<String> ls = new ArrayList<String>();
        HashMap<String,Integer> hm = new HashMap<String,Integer>();
        while(begin_window+10<=s.length()){
            String temp = s.substring(begin_window,begin_window+10);
                if ( hm.containsKey(temp)) { // check if key exists
                    Int newValue = hm.get(temp); // get value
                    newValue++;  // increase it
                    ls.add(temp); // add to your list
                    hm.put(temp,newValue);
                    System.out.println(hm.get(temp));
                } else {
                    hm.put(temp,1);
            }
            begin_window++;
        }
        return ls;
    }
    

    The JavaDoc gives you a quick overview of the Methods the HashMap has: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html