File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * 1653. Minimum Deletions to Make String Balanced
3+ *
4+ * 參數s中只有'a' & 'b'這兩個字母。
5+ * 刪除任一字母使s balanced,若不存在一對index (i,j) 使得 i < j 且 s[i] = 'b' 且 s[j] = 'a',則s 是balanced。
6+ * 回傳至少須刪除幾次(操作幾次)才能使s balanced
7+ *
8+ *
9+ * @param {string } s
10+ * @return {number }
11+ */
12+ var minimumDeletions = function ( s ) {
13+ // balanced string中,b不能出現在a之後
14+ // no such 'b' at s[i] where s[j] is 'a' and i < j
15+
16+ // TC:O(N)
17+ // 計算a,b各自出現幾次
18+ let countA = 0 , countB = 0 ;
19+ let minDel = s . length ;
20+ // 先計算a出現幾次
21+ for ( let i = 0 ; i < s . length ; ++ i ) {
22+ if ( s [ i ] === "a" ) {
23+ countA ++ ;
24+ }
25+ }
26+ // 之後再次迴圈,若遇到a則--
27+ for ( let i = 0 ; i < s . length ; ++ i ) {
28+ if ( s [ i ] === "a" ) {
29+ countA -- ;
30+ }
31+ // 不斷更新比較雙方次數
32+ minDel = Math . min ( minDel , countA + countB ) ;
33+
34+ // 遇到b,++
35+ if ( s [ i ] === "b" ) {
36+ countB ++ ;
37+ }
38+ }
39+ return minDel ;
40+ } ;
41+ let s = "aababbab" ;
42+ /*Output: 2
43+ Explanation: You can either:
44+ Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
45+ Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
46+ */
47+ console . log ( minimumDeletions ( s ) ) ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 3760. Maximum Substrings With Distinct Start
3+ * Difficulty:Medium
4+ *
5+ * @param {string } s
6+ * @return {number }
7+ */
8+ var maxDistinct = function ( s ) {
9+ // 計算字串中,若每個開頭是跟另一substring開頭不同的字母,可以有幾種組合
10+ // 計算每個字母出現次數
11+
12+ // let map = new Map();
13+ // for(let i = 0; i < s.length;++i) {
14+ // map = map.has(s[i]) ? map.set(s[i], map.get(s[i]) + 1) : map.set(s[i], 1);
15+ // }
16+ // return map.size;
17+
18+ // solution 2.
19+ /**
20+ * TC: O(N) =>
21+ * 將s弄成陣列,須loop所有元素,因此O(N)
22+ * new Set(...) 將每個元素插入set,add是O(1)但要做n次,因此O(N)
23+ * size 讀取長度,因此O(1)
24+ *
25+ * new Set([...s]) 需要loop並插入所有元素,所以整體是O(n)
26+ * */
27+ return new Set ( [ ...s ] ) . size ;
28+ } ;
29+ let s = "abab" ;
30+ // 2
31+ console . log ( maxDistinct ( s ) )
Original file line number Diff line number Diff line change 11// debugger
2- import { format } from 'node:path' ;
32import { ExecutionTimer } from './time.js' ;
43import assert from 'node:assert/strict' ;
54import { count } from 'node:console' ;
6- import { lchown } from 'node:fs' ;
75
86/*
9722. Generate Parentheses
@@ -1260,25 +1258,3 @@ var minRemoval = function(nums, k) {
12601258// console.log(minRemoval(nums,k));
12611259
12621260
1263- /**
1264- * 1653. Minimum Deletions to Make String Balanced
1265- *
1266- * 參數s中只有'a' & 'b'這兩個字母。
1267- * 刪除任一字母使s balanced,若不存在一對index (i,j) 使得 i < j 且 s[i] = 'b' 且 s[j] = 'a',則s 是balanced。
1268- * 回傳最小須刪除幾次才能使s balanced
1269- *
1270- *
1271- * @param {string } s
1272- * @return {number }
1273- */
1274- var minimumDeletions = function ( s ) {
1275-
1276- } ;
1277- // let s = "aababbab";
1278- /*Output: 2
1279- Explanation: You can either:
1280- Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
1281- Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
1282- */
1283- // console.log(minimumDeletions(s));
1284-
You can’t perform that action at this time.
0 commit comments