Easy to understand sliding window solution


  • 0
    K

    Here is a solution that involves using a sliding window of size 10.

    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            HashMap<String, Integer> map = new HashMap<>();
            for(int i = 0; i<=s.length()-10;i++){
                int count = map.getOrDefault(s.substring(i,i+10),0);
                map.put(s.substring(i,i+10), count+1);
            }
            List<String> res = new ArrayList<>();
            for(Map.Entry<String, Integer> st : map.entrySet()){
                if(st.getValue() >1){
                    res.add(st.getKey());
                }
            }
            return res;
        }
    }

Log in to reply
 

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