File tree Expand file tree Collapse file tree 1 file changed +8
-12
lines changed
Expand file tree Collapse file tree 1 file changed +8
-12
lines changed Original file line number Diff line number Diff line change 11// problem link https://leetcode.com/problems/maximum-number-of-balloons
22// time complexity O(n)
3+ // space complexity O(n)
34
45var maxNumberOfBalloons = function ( text ) {
56
67 const balloonCach = { } ;
7- const ballonSet = new Set ( ) ;
8- for ( let i = 0 ; i < text . length ; i ++ ) {
9- ballonSet . add ( text [ i ] ) ;
10- }
8+ const ballonSet = new Set ( text . split ( '' ) ) ;
9+
10+ for ( const char of text ) {
11+ if ( ! ballonSet . has ( char ) ) continue ;
1112
12- for ( let i = 0 ; i < text . length ; i ++ ) {
13- if ( ballonSet . has ( text [ i ] ) ) {
14- if ( balloonCach [ text [ i ] ] ) {
15- balloonCach [ text [ i ] ] += 1 ;
16- } else {
17- balloonCach [ text [ i ] ] = 1 ;
18- }
19- }
13+ const count = ( ( balloonCach [ char ] ?? 0 ) + 1 )
14+ balloonCach [ char ] = count ;
2015 }
2116
2217 let min = Math . min ( balloonCach [ 'b' ] ,
@@ -27,3 +22,4 @@ var maxNumberOfBalloons = function(text) {
2722
2823 return min ? min : 0 ;
2924} ;
25+
You can’t perform that action at this time.
0 commit comments