8
8
9
9
// ["a"]
10
10
// [["a"]]
11
-
11
+ /**
12
+ * @param {string[] } strs
13
+ * @return {string[][] }
14
+ */
12
15
const groupAnagrams = ( list ) => {
13
16
// result,
14
17
// shift -> for i & k=i+1
15
18
// twice isAnagram just to check
16
19
// if we find one -> splice(index, 1)
17
20
// -> group.push(item) // until the end of the list
18
21
// until []
19
- processWords ( list , [ ] , [ ] ) ;
22
+ // let x = processWords(list, []);
23
+ // console.log("x")
24
+ // console.log(x)
25
+ return processWords ( list , [ ] ) ;
20
26
}
21
27
22
28
const areTheyAnagrams = ( word1 , word2 ) => {
23
- if ( undefined === word2 ) return false ;
29
+ console . log ( ">>>" )
30
+ console . log ( word1 || "0" )
31
+ console . log ( word2 )
32
+ console . log ( "<<<" )
33
+ let char1 = ( word1 || "0" ) . split ( "" ) ,
34
+ char2 = ( word2 || "0" ) . split ( "" ) ;
35
+
36
+ if ( undefined === word2 || ( word1 && word2 && ( word1 . length !== word2 . length ) ) ) return false ;
37
+
38
+
39
+ let map1 = createAnagramMap ( char1 ) , map2 = createAnagramMap ( char2 ) ;
40
+ console . log ( "map1:map2" )
41
+ console . log ( map1 )
42
+ console . log ( map2 )
43
+
24
44
25
- let char = word1 . split ( "" ) , map1 = createAnagramMap ( char ) , map2 = createAnagramMap ( word2 . split ( "" ) ) ;
26
45
// {“e” => 1, “a” => 1, “t” => 1}
27
- for ( let i = 0 ; i < char . length ; i ++ ) {
28
- if ( map1 . get ( char [ i ] ) !== map2 . get ( char [ i ] ) ) return false ;
46
+ for ( let i = 0 ; i < char1 . length ; i ++ ) {
47
+ console . log ( ">>" )
48
+ console . log ( map1 . get ( char1 [ i ] ) !== map2 . get ( char1 [ i ] ) )
49
+ if ( map1 . get ( char1 [ i ] ) !== map2 . get ( char1 [ i ] ) ) return false ;
29
50
}
30
51
31
52
return true ;
@@ -41,26 +62,60 @@ const createAnagramMap = (char) => {
41
62
return map ;
42
63
}
43
64
44
- const processWords = ( list , groupList , group ) => {
45
- if ( [ ] === list ) {
65
+ const processWords = ( list , groupList ) => {
66
+ if ( 0 === list . length ) {
46
67
return groupList ;
47
68
}
48
69
49
- let curr = list . shift ( ) ; // ["eat","tea","tan","ate","nat","bat"] -> ["tea","tan","ate","nat","bat"]
50
- let tmpList = list ;
70
+ console . log ( "check" )
71
+ console . log ( list )
72
+ // console.log(list.length)
73
+ // console.log(groupList)
74
+ // console.log(group)
51
75
52
- while ( [ ] !== list ) {
76
+ // ["eat","tea","tan","ate","nat","bat"] -> ["tea","tan","ate","nat","bat"]
77
+ let curr = list . shift ( ) , tmpList = [ ...list ] , group = [ ] ;
78
+ group . push ( curr ) ;
79
+
80
+ while ( 0 < list . length ) {
53
81
let next = list . shift ( ) ;
54
- if ( areTheyAnagrams ( curr , next ) ) group . push ( next ) ;
82
+ console . log ( `=> comparing ${ curr } :${ next } ` )
83
+
84
+ if ( areTheyAnagrams ( curr , next ) ) {
85
+ console . log ( "they are" )
86
+ group . push ( next ) ;
87
+ }
55
88
}
56
89
57
- // remove from tmpList that are added into the group
58
- tmpList . filter ( ( item ) => {
59
- return ! group . includes ( item ) ;
90
+ console . log ( "=> group" )
91
+ console . log ( group )
92
+ // console.log("-> tmpList")
93
+ // console.log(tmpList)
94
+
95
+ // Remove from tmpList that are added into the group
96
+ tmpList = tmpList . filter ( ( elem ) => {
97
+ return ! group . includes ( elem ) ;
60
98
} )
61
99
100
+ // console.log("tmpList <-")
101
+ // console.log(tmpList)
102
+
62
103
// process for groupList
63
104
groupList . push ( group ) ;
105
+ console . log ( "groupList" )
106
+ console . log ( groupList )
64
107
65
- return processWords ( tmpList , groupList , group ) ;
108
+ return processWords ( tmpList , groupList ) ;
66
109
}
110
+
111
+ let x =
112
+ // groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
113
+ // groupAnagrams(["", "b"])
114
+ // groupAnagrams(["", ""])
115
+ groupAnagrams ( [ "tea" , "and" , "ace" , "ad" , "eat" , "dans" ] )
116
+ // groupAnagrams([""])
117
+ // groupAnagrams(["a"])
118
+ // groupAnagrams([])
119
+
120
+ console . log ( ">>" )
121
+ console . log ( x )
0 commit comments