HashMap Solution Java


  • 0
    S

    '''

    public class Solution {
    
    public List<String> findRepeatedDnaSequences(String s) {
    
        ArrayList <String> list = new ArrayList<String>();
        HashMap< String,Integer> map = new HashMap< String,Integer> ();
        for(int i = 0 ; i<s.length()-9; i++){
            String check = s.substring(i,i+10);
            if(!map.containsKey(check)) map.put(check,1);
            else map.put(check,map.get(check)+1);
    
    }
    
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
    
            if(entry.getValue() > 1) list.add(entry.getKey());
    

    }

     return list;
    

    }
    }


Log in to reply
 

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