File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
0200-0299/0219.Contains Duplicate II
3100-3199/3163.String Compression III Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } k
4+ * @return {boolean }
5+ */
6+ var containsNearbyDuplicate = function ( nums , k ) {
7+ const d = new Map ( ) ;
8+ for ( let i = 0 ; i < nums . length ; ++ i ) {
9+ if ( d . has ( nums [ i ] ) && i - d . get ( nums [ i ] ) <= k ) {
10+ return true ;
11+ }
12+ d . set ( nums [ i ] , i ) ;
13+ }
14+ return false ;
15+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } word
3+ * @return {string }
4+ */
5+ var compressedString = function ( word ) {
6+ const ans = [ ] ;
7+ const n = word . length ;
8+ for ( let i = 0 ; i < n ; ) {
9+ let j = i + 1 ;
10+ while ( j < n && word [ j ] === word [ i ] ) {
11+ ++ j ;
12+ }
13+ let k = j - i ;
14+ while ( k ) {
15+ const x = Math . min ( k , 9 ) ;
16+ ans . push ( x + word [ i ] ) ;
17+ k -= x ;
18+ }
19+ i = j ;
20+ }
21+ return ans . join ( '' ) ;
22+ } ;
You can’t perform that action at this time.
0 commit comments