Featured image of post 242. Valid Anagram

242. Valid Anagram

242. Valid Anagram

https://leetcode.com/problems/valid-anagram/description/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

Category	Difficulty	Likes	Dislikes
algorithms	Easy (64.48%)	11937	394
Tags
hash-table | sort

Companies
amazon | uber | yelp

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

 

Example 1:

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

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

Constraints:

1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.
 

Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?


/* 思路:哈希表
 *      异位词,即两个单词/短语s1, s2互为重排,其充要条件是
 *     1字符串的字符总数量,
 *     2以及每个字符对应的数量相同。
 *     因此可以使用哈希表分别统计字符串中各个字符数量 key:字符,value:数量,
 *     统计s1时,value+1, 统计s2时value-1,
 *    当最终哈希表中所有字符对应的统计数值为0,则互为重排。
 */
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @lc code=start

import java.util.HashMap;

class Solution {
    public boolean isAnagram(String s, String t) {

        HashMap<Character, Integer> dic = new HashMap<>();
        for(int i = 0; i <s.length(); i++){
            dic.put(s.charAt(i), dic.getOrDefault(s.charAt(i), 0)+1);  
        }
        for(int i = 0; i < t.length(); i++){
            dic.put(t.charAt(i), dic.getOrDefault(t.charAt(i), 0)-1);
        }
        for(int val : dic.values()){
            if(val !=0){
                return false;
            }
        }
        return true;
    }
}

// @lc code=end
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy