【Likou】242. Effective anagram <hash>

【Likou】242. Effective anagrams

Given two strings s and t, write a function to determine whether t is an anagram of s. Note: If each character in s and t appears the same number of times, then s and t are said to be anagrams of each other.

Example 1:
Input: s = "anagram", t = "nagaram"
Output: true

Example 2:
Input: s = "rat", t = "car"
Output: false

Hint:
1 <= s.length, t.length <= 5 * 104
s and t contain only lowercase letters

answer

class Solution {
    
    
    public boolean isAnagram(String s, String t) {
    
    
        int[] record = new int[26];

        for (int i = 0; i < s.length(); i++) {
    
    
            record[s.charAt(i) - 'a']++;
        }

        for (int i = 0; i < t.length(); i++) {
    
    
            record[t.charAt(i) - 'a']--;
        }

        for (int count: record) {
    
    
            if (count != 0) {
    
    
                return false;
            }
        }
        return true;
    }
}

Guess you like

Origin blog.csdn.net/qq_44033208/article/details/132494980