加载中...
1576-替换所有的问号(Replace All ?'s to Avoid Consecutive Repeating Characters)
发表于:2021-12-03 | 分类: 简单
字数统计: 851 | 阅读时长: 4分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters

英文原文

Given a string s containing only lowercase English letters and the '?' character, convert all the '?' characters into lowercase 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 consist of lowercase English letters and '?'.

中文题目

给你一个仅包含小写英文字母和 '?' 字符的字符串 s,请你将所有的 '?' 转换为若干小写字母,使最终的字符串不包含任何 连续重复 的字符。

注意:你 不能 修改非 '?' 字符。

题目测试用例保证 '?' 字符 之外,不存在连续重复的字符。

在完成所有转换(可能无需转换)后返回最终的字符串。如果有多个解决方案,请返回其中任何一个。可以证明,在给定的约束条件下,答案总是存在的。

 

示例 1:

输入:s = "?zs"
输出:"azs"
解释:该示例共有 25 种解决方案,从 "azs" 到 "yzs" 都是符合题目要求的。只有 "z" 是无效的修改,因为字符串 "zzs" 中有连续重复的两个 'z' 。

示例 2:

输入:s = "ubv?w"
输出:"ubvaw"
解释:该示例共有 24 种解决方案,只有替换成 "v" 和 "w" 不符合题目要求。因为 "ubvvw" 和 "ubvww" 都包含连续重复的字符。

示例 3:

输入:s = "j?qg??b"
输出:"jaqgacb"

示例 4:

输入:s = "??yw?ipkj?"
输出:"acywaipkja"

 

提示:

  • 1 <= s.length <= 100

  • s 仅包含小写英文字母和 '?' 字符

通过代码

高赞题解

image.png

解题思路

从前面开始遍历,遇到问号就对比前面和后面的 用一个不与前面和后面一致的字符代替就好了

代码

class Solution {
    public String modifyString(String s) {
        char[] chars = s.toCharArray();

        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '?') {
                //前面一个字符  如果当前是第0个的话 字符就为‘ ’
                char ahead = i == 0 ? ' ' : chars[i - 1];
                //后面一个字符  如果当前是最后一个的话 字符就为‘ ’
                char behind  = i == chars.length - 1 ? ' ' : chars[i + 1];
                //从a开始比较  如果等于前面或者后面的话 就+1
                char temp = 'a';
                while (temp == ahead || temp == behind ) {
                    temp++;
                }
                //找到目标字符后 做替换
                chars[i] = temp;
            }
        }
        return new String(chars);
    }
}

统计信息

通过次数 提交次数 AC比率
18484 38736 47.7%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
1591-奇怪的打印机 II(Strange Printer II)
下一篇:
1577-数的平方等于两数乘积的方法数(Number of Ways Where Square of Number Is Equal to Product of Two Numbers)
本文目录
本文目录