Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1576.html

1576. Replace All ?’s to Avoid Consecutive Repeating Characters (Easy)

Given a string s containing only lower case English letters and the '?' character, convert all the '?' characters into lower case letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non '?' characters.

It is guaranteed that there are no consecutive repeating characters in the given string except for '?'.

Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.

 

Example 1:

Input: s = "?zs"
Output: "azs"
Explanation: There are 25 solutions for this problem. From "azs" to "yzs", all are valid. Only "z" is an invalid modification as the string will consist of consecutive repeating characters in "zzs".

Example 2:

Input: s = "ubv?w"
Output: "ubvaw"
Explanation: There are 24 solutions for this problem. Only "v" and "w" are invalid modifications as the strings will consist of consecutive repeating characters in "ubvvw" and "ubvww".

Example 3:

Input: s = "j?qg??b"
Output: "jaqgacb"

Example 4:

Input: s = "??yw?ipkj?"
Output: "acywaipkja"

 

Constraints:

  • 1 <= s.length <= 100

  • s contains only lower case English letters and '?'.

Related Topics:
String

Solution 1.

When we see a s[i] == '?', we set it as s[i - 1] + 1 and round it to 'a' if necessary.

When s[i] == '?' && s[i + 1] != '?', there is a chance of conflict with s[i + 1]. If there is a conflict, we simply increment and round s[i] again.

// OJ: https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
// Time: O(N)
// Space: O(1)
class Solution {
public:
    string modifyString(string s) {
        int i = 0, N = s.size();
        while (i < N) {
            while (i < N && s[i] != '?') ++i;
            int c = (i == 0 ? 0 : (s[i - 1] - 'a' + 1) % 26);
            while (i < N && s[i] == '?') {
                s[i] = c + 'a';
                c = (c + 1) % 26;
                if (i + 1 < N && s[i + 1] != '?' && s[i] == s[i + 1]) s[i] = c + 'a';
            }
        }
        return s;
    }
};
  • class Solution {
        public String modifyString(String s) {
            char[] array = s.toCharArray();
            int length = array.length;
            if (array[0] == '?') {
                if (length == 1)
                    array[0] = 'a';
                else {
                    if (array[1] == 'a')
                        array[0] = 'b';
                    else
                        array[0] = 'a';
                }
            }
            if (array[length - 1] == '?') {
                if (array[length - 2] == 'a')
                    array[length - 1] = 'b';
                else
                    array[length - 1] = 'a';
            }
            for (int i = 1; i < length - 1; i++) {
                char curr = array[i];
                if (curr == '?') {
                    char prev = array[i - 1], next = array[i + 1];
                    char letter = 'a';
                    while (letter == prev || letter == next)
                        letter++;
                    array[i] = letter;
                }
            }
            return new String(array);
        }
    }
    
    ############
    
    class Solution {
        public String modifyString(String s) {
            char[] cs = s.toCharArray();
            int n = cs.length;
            for (int i = 0; i < n; ++i) {
                if (cs[i] == '?') {
                    for (char c = 'a'; c <= 'c'; ++c) {
                        if ((i > 0 && cs[i - 1] == c) || (i + 1 < n && cs[i + 1] == c)) {
                            continue;
                        }
                        cs[i] = c;
                        break;
                    }
                }
            }
            return String.valueOf(cs);
        }
    }
    
  • // OJ: https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/
    // Time: O(N)
    // Space: O(1)
    class Solution {
    public:
        string modifyString(string s) {
            int i = 0, N = s.size();
            while (i < N) {
                while (i < N && s[i] != '?') ++i;
                int c = (i == 0 ? 0 : (s[i - 1] - 'a' + 1) % 26);
                while (i < N && s[i] == '?') {
                    s[i] = c + 'a';
                    c = (c + 1) % 26;
                    if (i + 1 < N && s[i + 1] != '?' && s[i] == s[i + 1]) s[i] = c + 'a';
                }
            }
            return s;
        }
    };
    
  • class Solution:
        def modifyString(self, s: str) -> str:
            ans = list(s)
            for i, c in enumerate(ans):
                if c == '?':
                    for cc in 'abc':
                        if i > 0 and ans[i - 1] == cc:
                            continue
                        if i < len(s) - 1 and ans[i + 1] == cc:
                            continue
                        ans[i] = cc
                        break
            return ''.join(ans)
    
    
    
  • func modifyString(s string) string {
    	n := len(s)
    	cs := []byte(s)
    	for i := range s {
    		if cs[i] == '?' {
    			for c := byte('a'); c <= byte('c'); c++ {
    				if (i > 0 && cs[i-1] == c) || (i+1 < n && cs[i+1] == c) {
    					continue
    				}
    				cs[i] = c
    				break
    			}
    		}
    	}
    	return string(cs)
    }
    
  • function modifyString(s: string): string {
        const cs = s.split('');
        const n = s.length;
        for (let i = 0; i < n; ++i) {
            if (cs[i] === '?') {
                for (const c of 'abc') {
                    if (
                        (i > 0 && cs[i - 1] === c) ||
                        (i + 1 < n && cs[i + 1] === c)
                    ) {
                        continue;
                    }
                    cs[i] = c;
                    break;
                }
            }
        }
        return cs.join('');
    }
    
    

All Problems

All Solutions