Easy Java Soln


  • 0
    A

    //Put all gene fronmt he bank into a HashSet for O(1) check
    HashSet HS=new HashSet();
    for (int i=0;i<bank.length;i++)
    HS.add(bank[i]);
    int cnt=0;
    String temp="";
    for (int i=0;i<start.length();i++){
    //System.out.println("start======>"+start);
    if (start.charAt(i)!=end.charAt(i)){
    temp=start.substring(0,i)+end.substring(i);//+start.substring(i+1);
    //System.out.println("temp======>"+temp);
    if (HS.contains(temp)){
    cnt++;
    }
    else{
    return -1;
    }
    start=start.substring(0,i)+end.substring(i,i+1)+start.substring(i+1);
    }
    }

        return cnt;

Log in to reply
 

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