Skip to content

Commit 8a86dd8

Browse files
committed
Valid Anagram
1 parent e4d71ba commit 8a86dd8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

โ€Žvalid-anagram/forest000014.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
s์™€ t๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ, ์นด์šดํŒ…์„ ์œ„ํ•ด 26๊ฐœ์˜ ๊ณ ์ •๋œ key๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ถฉ๋ถ„ํ•˜๊ณ , ๋ฐฐ์—ด์ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ณ  ์ ํ•ฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•จ
3+
4+
Runtime: 4 ms (Beats: 76.59%)
5+
Time Complexity: O(n)
6+
7+
Memory: 43.04 MB (Beats: 78.65%)
8+
Space Complexity: O(1)
9+
*/
10+
11+
class Solution {
12+
public boolean isAnagram(String s, String t) {
13+
if (s.length() != t.length())
14+
return false;
15+
16+
int[] cnt = new int[26];
17+
18+
for (int i = 0; i < s.length(); i++) {
19+
cnt[s.charAt(i) - 'a']++;
20+
}
21+
for (int i = 0; i < t.length(); i++) {
22+
cnt[t.charAt(i) - 'a']--;
23+
}
24+
25+
for (int i = 0; i < 26; i++) {
26+
if (cnt[i] != 0)
27+
return false;
28+
}
29+
30+
return true;
31+
}
32+
}

0 commit comments

Comments
ย (0)