Leetcode题解 242. Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.

简单题,《程序员面试金典》原题

public static boolean isAnagram(String s, String t) {
        int[] a = new int[256];
        int[] b = new int[256];
        for (int i = 0; i < s.length(); i++) {
            a[s.charAt(i)]++;
        }
        for (int i = 0; i < t.length(); i++) {
            b[t.charAt(i)]++;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }

你可能感兴趣的:(LeetCode)