Skip to content

Latest commit

 

History

History
170 lines (141 loc) · 4.86 KB

File metadata and controls

170 lines (141 loc) · 4.86 KB

中文文档

Description

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 '?'.

Solutions

Python3

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)

Java

class Solution {
    public String modifyString(String s) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            char c = chars[i];
            if (c == '?') {
                for (char cc = 'a'; cc <= 'c'; ++cc) {
                    if (i > 0 && chars[i - 1] == cc) {
                        continue;
                    }
                    if (i < chars.length - 1 && chars[i + 1] == cc) {
                        continue;
                    }
                    chars[i] = cc;
                    break;
                }
            }
        }
        return String.valueOf(chars);
    }
}

C++

class Solution {
public:
    string modifyString(string s) {
        for (int i = 0; i < s.size(); ++i)
        {
            if (s[i] == '?')
            {
                for (char cc : "abc")
                {
                    if (i > 0 && s[i - 1] == cc) continue;
                    if (i < s.size() - 1 && s[i + 1] == cc) continue;
                    s[i] = cc;
                    break;
                }
            }
        }
        return s;
    }
};

Go

func modifyString(s string) string {
	ans := []byte(s)
	for i, c := range ans {
		if c == '?' {
			for cc := byte('a'); cc <= 'c'; cc++ {
				if i > 0 && ans[i-1] == cc {
					continue
				}
				if i < len(s)-1 && ans[i+1] == cc {
					continue
				}
				ans[i] = cc
				break
			}
		}
	}
	return string(ans)
}

TypeScript

function modifyString(s: string): string {
    const strArr = s.split("");
    const n = s.length;
    for (let i = 0; i < n; i++) {
        if (strArr[i] === "?") {
            const before = strArr[i - 1];
            const after = strArr[i + 1];

            if (after !== "a" && before !== "a") {
                strArr[i] = "a";
            } else if (after !== "b" && before !== "b") {
                strArr[i] = "b";
            } else {
                strArr[i] = "c";
            }
        }
    }
    return strArr.join("");
}