The most violent solution


  • 0
    1

    public class Solution {
    public List<String> findRepeatedDnaSequences(String s) {
    List<String> result = new ArrayList<>();
    if (s == null || s.length() < 10) {
    return result;
    }

        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 9; i < s.length(); i++) {
            String subString = s.substring(i - 9, i + 1);
            if (map.containsKey(subString)) {
                int freq = map.get(subString);
                map.put(subString, ++freq);
            } else {
                map.put(subString, 1);
            }
        }
        
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() > 1) {
                result.add(entry.getKey());
            }
        }
        return result;
    }
    

    }


Log in to reply
 

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