File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ var groupAnagrams = function ( strs ) {
2
+ // Declare hash map to store sorted strs
3
+ let map = new Map ( ) ;
4
+
5
+ for ( let str of strs ) {
6
+ // Sorted each str
7
+ const sortedStr = str . split ( "" ) . sort ( ) . join ( "" ) ;
8
+
9
+ // If there is alread sortedStr on the map, pushed str
10
+ if ( map . has ( sortedStr ) ) {
11
+ map . get ( sortedStr ) . push ( str ) ;
12
+ } else {
13
+ // If there is no sortedStr on the map, insert [str]
14
+ map . set ( sortedStr , [ str ] ) ;
15
+ }
16
+ }
17
+ return Array . from ( map . values ( ) ) ;
18
+ } ;
19
+
20
+ // TC: O(n*klogk)
21
+ // SC: O(n*k)
22
+ // n -> length of strs array
23
+ // k -> amount of character for each element
Original file line number Diff line number Diff line change
1
+ var missingNumber = function ( nums ) {
2
+ // Get a expected summation
3
+ const n = nums . length ;
4
+ const expectedSum = ( n * ( n + 1 ) ) / 2 ;
5
+
6
+ // Calculate summation of nums
7
+ let numsSum = 0 ;
8
+ for ( let i = 0 ; i < n ; i ++ ) {
9
+ numsSum += nums [ i ] ;
10
+ }
11
+
12
+ return expectedSum - numsSum ;
13
+ } ;
14
+
15
+ // TC: O(n)
16
+ // SC: O(1)
You can’t perform that action at this time.
0 commit comments