Help!Wrong Answer Using hashCode() Java


  • 0
    J

    I'm using hashcode to identify the same String. however it fails on the last input I believe, which output["CTGTTCGTTG"], but when I'm testing using my own pc, it output nothing which is correct answer. Could anyone help me?

    public List<String> findRepeatedDnaSequences(String s) {
    	int length=s.length();
    	if(length<=10){
    		List<String>res=new ArrayList<String>();
    		if(length==10)
    			res.add(s);
    		return res;
    	}
    	Set<String> set= new HashSet<>();
    	HashMap<Integer,Integer> map=new HashMap<>();		
    	int start=0;
    	while(start+10<=length){
    		String s1=s.substring(start,start+10);
    		if(map.containsKey(s1.hashCode()))
    			set.add(s1);
    		else{
    			map.put(s1.hashCode(),1);
    		}
    		start++;
    	}
        return new ArrayList<String>(set);
    }

  • 3
    Y

    Collisions could happen in String.hashCode().
    "CTGTTCGTTG".hashCode()=="ACCCCTGAGG".hashCode().


  • 0
    E

    Thanks for the answer. Just curious, the string "CTGTTCGTTG" is not in the original input. Where does the "CTGTTCGTTG" come from? Would that be a bug in leetcode?


Log in to reply
 

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