forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path242.valid-anagram.java
More file actions
53 lines (51 loc) · 1.11 KB
/
242.valid-anagram.java
File metadata and controls
53 lines (51 loc) · 1.11 KB
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
43
44
45
46
47
48
49
50
51
52
/*
* @lc app=leetcode id=242 lang=java
*
* [242] Valid Anagram
*
* https://leetcode.com/problems/valid-anagram/description/
*
* algorithms
* Easy (57.26%)
* Likes: 1945
* Dislikes: 153
* Total Accepted: 666.5K
* Total Submissions: 1.2M
* Testcase Example: '"anagram"\n"nagaram"'
*
* Given two strings s and t , write a function to determine if t is an anagram
* of s.
*
* Example 1:
*
*
* Input: s = "anagram", t = "nagaram"
* Output: true
*
*
* Example 2:
*
*
* Input: s = "rat", t = "car"
* Output: false
*
*
* Note:
* You may assume the string contains only lowercase alphabets.
*
* Follow up:
* What if the inputs contain unicode characters? How would you adapt your
* solution to such case?
*
*/
// @lc code=start
class Solution {
public boolean isAnagram(String s, String t) {
int[] alphabet = new int[26];
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
for (int i : alphabet) if (i != 0) return false;
return true;
}
}
// @lc code=end