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

Commit ee118b0

Browse files
committed
Add solution #1576
1 parent a6b0ee8 commit ee118b0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 1576. Replace All ?'s to Avoid Consecutive Repeating Characters
3+
* https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s containing only lowercase English letters and the '?' character, convert all
7+
* the '?' characters into lowercase letters such that the final string does not contain any
8+
* consecutive repeating characters. You cannot modify the non '?' characters.
9+
*
10+
* It is guaranteed that there are no consecutive repeating characters in the given string except
11+
* for '?'.
12+
*
13+
* Return the final string after all the conversions (possibly zero) have been made. If there is
14+
* more than one solution, return any of them. It can be shown that an answer is always possible
15+
* with the given constraints.
16+
*/
17+
18+
/**
19+
* @param {string} s
20+
* @return {string}
21+
*/
22+
var modifyString = function(s) {
23+
const substitute = takenCharacteres => {
24+
for (let charCode = 97; ; charCode++) {
25+
const attempt = String.fromCharCode(charCode);
26+
27+
if (!takenCharacteres.includes(attempt)) {
28+
return attempt;
29+
}
30+
}
31+
};
32+
33+
const result = [...s];
34+
for (let index = 0; index < s.length; index++) {
35+
const takenCharacteres = [result[index - 1], result[index + 1]];
36+
result[index] = s[index] === '?' ? substitute(takenCharacteres) : s[index];
37+
}
38+
return result.join('');
39+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
1550|[Three Consecutive Odds](./1550-three-consecutive-odds.js)|Easy|
248248
1551|[Minimum Operations to Make Array Equal](./1551-minimum-operations-to-make-array-equal.js)|Medium|
249249
1566|[Detect Pattern of Length M Repeated K or More Times](./1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
250+
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
250251
1598|[Crawler Log Folder](./1598-crawler-log-folder.js)|Easy|
251252
1668|[Maximum Repeating Substring](./1668-maximum-repeating-substring.js)|Easy|
252253
1669|[Merge In Between Linked Lists](./1669-merge-in-between-linked-lists.js)|Medium|

0 commit comments

Comments
 (0)