My Java Solution Anagram


  • 0
    I
    public boolean isAnagram(String s, String t) {
        boolean is = false;
    	is = equal(count(s, alfabet()), count(t, alfabet()));
    	return is;
    }
    
    public static char[] alfabet() {
    	char[] alfabet = new char[26];
    	int i = 0;
    	for (char j = 'a'; j <= 'z'; j++) {
    		alfabet[i] = j;
    		i++;
    	}
    	return alfabet;
    }
    
    public static boolean equal(int[] a, int[] b){
    	boolean iseq = true;
    	for (int i = 0; i < a.length; i++) {
    		if (a[i]!=b[i]) {iseq = false; break;}
    	}
    	return iseq;
    	
    }
    
    public static int[] count(String g, char[] alfabet) {
    	int[] count = new int[26];
    	char[] all = g.toCharArray();
    
    	for (int i = 0; i < g.length(); i++) {
    		for (int j = 0; j < alfabet.length; j++) {
    			if (all[i] == alfabet[j]) {
    				count[j]++;
    			}
    		}
    	}
    	return count;
    
    }

Log in to reply
 

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