File tree Expand file tree Collapse file tree 1 file changed +84
-0
lines changed Expand file tree Collapse file tree 1 file changed +84
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * @lc app=leetcode.cn id=1576 lang=golang
3+ *
4+ * [1576] 替换所有的问号
5+ *
6+ * https://leetcode-cn.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/description/
7+ *
8+ * algorithms
9+ * Easy (47.67%)
10+ * Likes: 95
11+ * Dislikes: 0
12+ * Total Accepted: 42.8K
13+ * Total Submissions: 83.4K
14+ * Testcase Example: '"?zs"'
15+ *
16+ * 给你一个仅包含小写英文字母和 '?' 字符的字符串 s,请你将所有的 '?' 转换为若干小写字母,使最终的字符串不包含任何 连续重复 的字符。
17+ *
18+ * 注意:你 不能 修改非 '?' 字符。
19+ *
20+ * 题目测试用例保证 除 '?' 字符 之外,不存在连续重复的字符。
21+ *
22+ * 在完成所有转换(可能无需转换)后返回最终的字符串。如果有多个解决方案,请返回其中任何一个。可以证明,在给定的约束条件下,答案总是存在的。
23+ *
24+ *
25+ *
26+ * 示例 1:
27+ *
28+ * 输入:s = "?zs"
29+ * 输出:"azs"
30+ * 解释:该示例共有 25 种解决方案,从 "azs" 到 "yzs" 都是符合题目要求的。只有 "z" 是无效的修改,因为字符串 "zzs"
31+ * 中有连续重复的两个 'z' 。
32+ *
33+ * 示例 2:
34+ *
35+ * 输入:s = "ubv?w"
36+ * 输出:"ubvaw"
37+ * 解释:该示例共有 24 种解决方案,只有替换成 "v" 和 "w" 不符合题目要求。因为 "ubvvw" 和 "ubvww" 都包含连续重复的字符。
38+ *
39+ *
40+ * 示例 3:
41+ *
42+ * 输入:s = "j?qg??b"
43+ * 输出:"jaqgacb"
44+ *
45+ *
46+ * 示例 4:
47+ *
48+ * 输入:s = "??yw?ipkj?"
49+ * 输出:"acywaipkja"
50+ *
51+ *
52+ *
53+ *
54+ * 提示:
55+ *
56+ *
57+ *
58+ * 1 <= s.length <= 100
59+ *
60+ *
61+ * s 仅包含小写英文字母和 '?' 字符
62+ *
63+ *
64+ *
65+ */
66+
67+ // @lc code=start
68+ func modifyString (s string ) string {
69+ res := []byte (s )
70+ for k ,v := range s {
71+ if v != '?' {
72+ continue
73+ }
74+ for b := byte ('a' );b <= 'c' ;b ++ {
75+ if ! (k - 1 >= 0 && res [k - 1 ] == b || k + 1 < len (s ) && b == res [k + 1 ]){
76+ res [k ] = b
77+ break
78+ }
79+ }
80+ }
81+ return string (res )
82+ }
83+ // @lc code=end
84+
You can’t perform that action at this time.
0 commit comments