File tree Expand file tree Collapse file tree 2 files changed +80
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 2 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {number } k
4+ * @return {number }
5+ */
6+ var characterReplacement = function ( s , k ) {
7+ let left = 0 ;
8+ let maxCount = 0 ;
9+ const freq = new Array ( 26 ) . fill ( 0 ) ; // 알파벳 빈도수 저장
10+
11+ let result = 0 ;
12+
13+ for ( let right = 0 ; right < s . length ; right ++ ) {
14+ const idx = s . charCodeAt ( right ) - 65 ; // A ~ Z -> 0 ~ 25
15+ freq [ idx ] ++ ;
16+ maxCount = Math . max ( maxCount , freq [ idx ] ) ;
17+
18+ // 현재 윈도우 크기 - 가장 많은 문자 수 > k 면 왼쪽 포인터 이동
19+ while ( ( right - left + 1 ) - maxCount > k ) {
20+ freq [ s . charCodeAt ( left ) - 65 ] -- ;
21+ left ++ ;
22+ }
23+
24+ result = Math . max ( result , right - left + 1 ) ;
25+ }
26+
27+ return result ;
28+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {string } t
4+ * @return {string }
5+ */
6+ var minWindow = function ( s , t ) {
7+ if ( s . length < t . length ) return "" ;
8+
9+ // 1. t의 문자 개수를 세기
10+ const targetMap = new Map ( ) ;
11+ for ( let char of t ) {
12+ targetMap . set ( char , ( targetMap . get ( char ) || 0 ) + 1 ) ;
13+ }
14+
15+ let left = 0 ;
16+ let right = 0 ;
17+ let minLen = Infinity ;
18+ let minStart = 0 ;
19+
20+ let required = targetMap . size ; // 만족시켜야 할 문자 종류 수
21+ let formed = 0 ;
22+ const windowMap = new Map ( ) ;
23+
24+ while ( right < s . length ) {
25+ const char = s [ right ] ;
26+ windowMap . set ( char , ( windowMap . get ( char ) || 0 ) + 1 ) ;
27+
28+ // 문자 개수가 딱 맞는 경우
29+ if ( targetMap . has ( char ) && windowMap . get ( char ) === targetMap . get ( char ) ) {
30+ formed ++ ;
31+ }
32+
33+ // 모든 문자가 충족될 경우 -> 왼쪽 포인터 당겨보기
34+ while ( left <= right && formed === required ) {
35+ if ( right - left + 1 < minLen ) {
36+ minLen = right - left + 1 ;
37+ minStart = left ;
38+ }
39+
40+ const leftChar = s [ left ] ;
41+ windowMap . set ( leftChar , windowMap . get ( leftChar ) - 1 ) ;
42+ if ( targetMap . has ( leftChar ) && windowMap . get ( leftChar ) < targetMap . get ( leftChar ) ) {
43+ formed -- ;
44+ }
45+ left ++ ;
46+ }
47+
48+ right ++ ;
49+ }
50+
51+ return minLen === Infinity ? "" : s . slice ( minStart , minStart + minLen ) ;
52+ } ;
You can’t perform that action at this time.
0 commit comments