Really Easy To Understand Java, O(n)


  • 0
    G
    public List<String> findRepeatedDnaSequences(String s) {
            Map<String, Integer> m = new HashMap<String, Integer>();
            List<String> list = new LinkedList<String>();
            if(s.length() <= 10) return list; 
            for(int i = 0; i <= s.length() - 10; i++) {
                if(m.containsKey(s.substring(i, i + 10))) {
                   if(m.get(s.substring(i, i + 10)) == 1) {
                       list.add(s.substring(i, i + 10));
                       m.put(s.substring(i, i + 10), 2);
                   }
                }
                else m.put(s.substring(i, i + 10), 1);
            }
            return list;
        }

Log in to reply
 

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