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+ * map을 통해 문자의 개수를 저장하고, 두 문자열의 문자 개수를 비교해서 다른 값이 있다면 false를 반환하도록 풀이를작성했습니다
9+ *
10+ */
11+ var isAnagram = function ( s , t ) {
12+ if ( s . length !== t . length ) return false ;
13+
14+ const length = s . length ;
15+
16+ const stringMap = new Map ( ) ;
17+ const targetMap = new Map ( ) ;
18+
19+ for ( let i = 0 ; i < length ; i ++ ) {
20+
21+ const stringChar = s [ i ] ;
22+ const currentStringMap = stringMap . get ( stringChar ) ;
23+
24+ if ( currentStringMap ) {
25+ stringMap . set ( stringChar , currentStringMap + 1 )
26+ } else {
27+ stringMap . set ( stringChar , 1 )
28+ }
29+
30+ const targetChar = t [ i ] ;
31+ const currentTargetMap = targetMap . get ( targetChar ) ;
32+
33+ if ( currentTargetMap ) {
34+ targetMap . set ( targetChar , currentTargetMap + 1 )
35+ } else {
36+ targetMap . set ( targetChar , 1 )
37+ }
38+ }
39+
40+ for ( const [ char , count ] of [ ...stringMap ] ) {
41+ const targetCount = targetMap . get ( char ) ;
42+
43+ if ( ! targetCount ) return false ;
44+ if ( targetCount !== count ) return false ;
45+ }
46+
47+ return true
48+ } ;
You can’t perform that action at this time.
0 commit comments