本文主要是介绍Replace All ?‘s to Avoid Consecutive Repeating Characters,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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"
思路:这个题就是填进去,然后verify左右两边 ,找个合适的放进去,然后go on;
class Solution {public String modifyString(String s) {if(s == null || s.length() == 0) {return s;}char[] ss = s.toCharArray();for(int i = 0; i < ss.length; i++) {if(ss[i] == '?') {for(char c = 'a'; c <= 'z'; c++) {if(i > 0 && ss[i - 1] == c || i < ss.length - 1 && ss[i + 1] == c) {continue;} else {ss[i] = c;break;}}}}return new String(ss);}
}
这篇关于Replace All ?‘s to Avoid Consecutive Repeating Characters的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!