Python AC but Java MLE


  • 0
    H

    Can someone tell me what is the problem? Really need to know..

    Python code

    class Solution:
        # @param {string} s
        # @return {string[]}
        def findRepeatedDnaSequences(self, s):
            sets = set([])
            res = set([])
            for i in range(len(s)-9):
                tmp = s[i:(i+10)]
                if tmp in sets:
                    res.add(tmp)
                else:
                    sets.add(tmp)
            return list(res)
    

    Equivalent Java code:

    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            List<String> res = new ArrayList<>();
            Set<String> resset = new HashSet<>(), has = new HashSet<>();
            for(int i = 0; i < s.length() - 9; i++){
                String tmp = s.substring(i, i+10);
                if(has.contains(tmp)){
                    resset.add(tmp);
                }else{
                    has.add(tmp);
                }
            }
            for(String i: resset){
                res.add(i);
            }
            return res;
        }
    }

Log in to reply
 

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