Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2e770ad

Browse files
committed
Add solution #2645
1 parent 3430ee4 commit 2e770ad

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,7 @@
19651965
2641|[Cousins in Binary Tree II](./solutions/2641-cousins-in-binary-tree-ii.js)|Medium|
19661966
2643|[Row With Maximum Ones](./solutions/2643-row-with-maximum-ones.js)|Easy|
19671967
2644|[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|
19681969
2648|[Generate Fibonacci Sequence](./solutions/2648-generate-fibonacci-sequence.js)|Easy|
19691970
2649|[Nested Array Generator](./solutions/2649-nested-array-generator.js)|Medium|
19701971
2650|[Design Cancellable Function](./solutions/2650-design-cancellable-function.js)|Hard|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
};

0 commit comments

Comments
 (0)