File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 196519652641|[ Cousins in Binary Tree II] ( ./solutions/2641-cousins-in-binary-tree-ii.js ) |Medium|
196619662643|[ Row With Maximum Ones] ( ./solutions/2643-row-with-maximum-ones.js ) |Easy|
196719672644|[ Find the Maximum Divisibility Score] ( ./solutions/2644-find-the-maximum-divisibility-score.js ) |Easy|
1968+ 2645|[ Minimum Additions to Make Valid String] ( ./solutions/2645-minimum-additions-to-make-valid-string.js ) |Medium|
196819692648|[ Generate Fibonacci Sequence] ( ./solutions/2648-generate-fibonacci-sequence.js ) |Easy|
196919702649|[ Nested Array Generator] ( ./solutions/2649-nested-array-generator.js ) |Medium|
197019712650|[ Design Cancellable Function] ( ./solutions/2650-design-cancellable-function.js ) |Hard|
Original file line number Diff line number Diff line change 1+ /**
2+ * 2645. Minimum Additions to Make Valid String
3+ * https://leetcode.com/problems/minimum-additions-to-make-valid-string/
4+ * Difficulty: Medium
5+ *
6+ * Given a string word to which you can insert letters "a", "b" or "c" anywhere and any number
7+ * of times, return the minimum number of letters that must be inserted so that word becomes valid.
8+ *
9+ * A string is called valid if it can be formed by concatenating the string "abc" several times.
10+ */
11+
12+ /**
13+ * @param {string } word
14+ * @return {number }
15+ */
16+ var addMinimum = function ( word ) {
17+ let result = 0 ;
18+ let index = 0 ;
19+
20+ while ( index < word . length ) {
21+ if ( word [ index ] === 'a' ) {
22+ if ( word [ index + 1 ] === 'b' && word [ index + 2 ] === 'c' ) {
23+ index += 3 ;
24+ } else if ( word [ index + 1 ] === 'b' ) {
25+ result += 1 ;
26+ index += 2 ;
27+ } else if ( word [ index + 1 ] === 'c' ) {
28+ result += 1 ;
29+ index += 2 ;
30+ } else {
31+ result += 2 ;
32+ index += 1 ;
33+ }
34+ } else if ( word [ index ] === 'b' ) {
35+ if ( word [ index + 1 ] === 'c' ) {
36+ result += 1 ;
37+ index += 2 ;
38+ } else {
39+ result += 2 ;
40+ index += 1 ;
41+ }
42+ } else {
43+ result += 2 ;
44+ index += 1 ;
45+ }
46+ }
47+
48+ return result ;
49+ } ;
You can’t perform that action at this time.
0 commit comments