File tree 2 files changed +40
-0
lines changed
2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 247
247
1550|[ Three Consecutive Odds] ( ./1550-three-consecutive-odds.js ) |Easy|
248
248
1551|[ Minimum Operations to Make Array Equal] ( ./1551-minimum-operations-to-make-array-equal.js ) |Medium|
249
249
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|
250
251
1598|[ Crawler Log Folder] ( ./1598-crawler-log-folder.js ) |Easy|
251
252
1668|[ Maximum Repeating Substring] ( ./1668-maximum-repeating-substring.js ) |Easy|
252
253
1669|[ Merge In Between Linked Lists] ( ./1669-merge-in-between-linked-lists.js ) |Medium|
You can’t perform that action at this time.
0 commit comments