File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {string } t
4+ * @return {boolean }
5+ */
6+
7+ /**
8+ * Runtime: 68ms, Memory: 54.49MB
9+ * n = s.length > t.length ? s.length : t.length
10+ * Time complexity: O(n)
11+ * Space complexity: O(n)
12+ *
13+ * **/
14+
15+ function arrayToDict ( arr ) {
16+ const dict = { } ;
17+ for ( let element of arr ) {
18+ if ( dict [ element ] ) {
19+ dict [ element ] += 1 ;
20+ } else {
21+ dict [ element ] = 1 ;
22+ }
23+ }
24+ return dict ;
25+ }
26+
27+ function isSameDict ( dict1 , dict2 ) {
28+ if ( Object . keys ( dict1 ) . length !== Object . keys ( dict2 ) . length ) {
29+ return false ;
30+ }
31+
32+ for ( const elem in dict1 ) {
33+ if ( dict1 [ elem ] !== dict2 [ elem ] ) {
34+ return false ;
35+ }
36+ }
37+ return true ;
38+ }
39+
40+ var isAnagram = function ( s , t ) {
41+ const sArr = [ ...s ] ;
42+ const tArr = [ ...t ] ;
43+
44+ const sDict = arrayToDict ( sArr ) ;
45+ const tDict = arrayToDict ( tArr ) ;
46+
47+ return isSameDict ( sDict , tDict ) ;
48+ } ;
You can’t perform that action at this time.
0 commit comments