File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 242. Valid Anagram
3
+ * Given two strings s and t, return true if t is an anagram of s, and false otherwise.
4
+ *
5
+ * https://leetcode.com/problems/valid-anagram/description/
6
+ */
7
+ function isAnagram ( s : string , t : string ) : boolean {
8
+ if ( s . length !== t . length ) {
9
+ return false ;
10
+ }
11
+
12
+ const charMap = new Map < string , number > ( ) ;
13
+
14
+ for ( const char of s ) {
15
+ const count = charMap . get ( char ) ;
16
+ if ( count ) {
17
+ charMap . set ( char , count + 1 ) ;
18
+ } else {
19
+ charMap . set ( char , 1 ) ;
20
+ }
21
+ }
22
+
23
+ for ( const char of t ) {
24
+ const count = charMap . get ( char ) ;
25
+ if ( count ) {
26
+ charMap . set ( char , count - 1 ) ;
27
+ } else {
28
+ return false ;
29
+ }
30
+ }
31
+
32
+ return true ;
33
+ }
34
+
35
+ // O(n) time
36
+ // O(n) space
You can’t perform that action at this time.
0 commit comments