This code isn't working and I don't know why?


  • 1
    Y

    I even went all the way to storing the sorted char arrays in custom object so that I can implement the hashCode and equals. The solution is so simple and should work, and I use Arrays.sort which can't be wrong (no one needs to implement sorting after taking the algorithms course). Anyway, I'm getting wrong answer and I don't know why. Do you?

    public class Solution {
        public static class SortedCharArray {
            char [] sorted;
            
            public SortedCharArray(String s) {
                if(s == null || s.length() == 0){
                    throw new IllegalArgumentException();
                }
                sorted = s.toCharArray();
                Arrays.sort(sorted);
            }
            
            public int hashCode() {
                int hash = Character.getNumericValue(sorted[0]);
                for(int i = 1; i < sorted.length; ++i){
                    hash += Character.getNumericValue(sorted[i]) * (int) Math.pow(31, i);
                }
                return hash;
            }
            
            public boolean equals(SortedCharArray other) {
                return other != null && Arrays.equals(this.sorted, other.sorted);
            }
             
        }
        public ArrayList<String> anagrams(String[] strs) {
            ArrayList<String> retval = new ArrayList<String>(strs.length);
            
            if(strs == null || strs.length == 0) 
                return retval;
            
            int emptyStringCount = 0;
            Map<SortedCharArray, List<String>> sortedToOrigs = new HashMap<SortedCharArray, List<String>>();
            for(String s: strs) {
                if(s.isEmpty()){
                    ++emptyStringCount;
                    continue;
                }
                SortedCharArray sorted = new SortedCharArray(s);
                List<String> origs = sortedToOrigs.get(sorted);
                if(origs == null) {
                    origs = new LinkedList();
                    sortedToOrigs.put(sorted, origs);
                }
                origs.add(0,s);
            }
            
            for(List<String> origs: sortedToOrigs.values()) {
                if(origs.size() == 1) 
                    continue;
                
                retval.addAll(origs);
                
            }
            
            if(emptyStringCount > 1){
                retval.addAll(Collections.nCopies(emptyStringCount, ""));
            }
            return retval;
        }
    

    }


  • 1
    H

    haha!
    You are very excellent. I see that you can use inner class and overide equals and hashCode method of Objects. But you forget a import thing. The rule of overide. I debug the program, and see that the jvm doesn't call your equals meghod. You should write your equals method by equals(Object obj);
    here is the acc code after my modification.

    public class Solution {
    public static class SortedCharArray {
        char [] sorted;
    
        public SortedCharArray(String s) {
            if(s == null || s.length() == 0){
                throw new IllegalArgumentException();
            }
            sorted = s.toCharArray();
            Arrays.sort(sorted);
        }
    
        public int hashCode() {
            int hash = Character.getNumericValue(sorted[0]);
            for(int i = 1; i < sorted.length; ++i){
                hash += Character.getNumericValue(sorted[i]) * (int) Math.pow(31, i);
            }
            return hash;
        }
       //the change place
       public boolean equals(Object obj) {
        	SortedCharArray other = (SortedCharArray)obj;
            return other != null && Arrays.equals(this.sorted, other.sorted);
        }
    
    }
    public ArrayList<String> anagrams(String[] strs) {
        ArrayList<String> retval = new ArrayList<String>(strs.length);
    
        if(strs == null || strs.length == 0) 
            return retval;
    
        int emptyStringCount = 0;
        
        Map<SortedCharArray, List<String>> sortedToOrigs = new HashMap<SortedCharArray, List<String>>();
        for(String s: strs) {
            if(s.isEmpty()){
                ++emptyStringCount;
                continue;
            }
            // get the class 
            SortedCharArray sorted = new SortedCharArray(s);
            List<String> origs = sortedToOrigs.get(sorted);
            if(origs == null) {
                origs = new LinkedList<String>();
                sortedToOrigs.put(sorted, origs);
            }
            origs.add(0,s);
        }
    
        for(List<String> origs: sortedToOrigs.values()) {
            if(origs.size() == 1) 
                continue;
    
            retval.addAll(origs);
        }
    
        if(emptyStringCount > 1){
            retval.addAll(Collections.nCopies(emptyStringCount, ""));
        }
        return retval;
    }}
    

Log in to reply
 

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