-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
59 lines (49 loc) · 1.73 KB
/
Solution.cs
File metadata and controls
59 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Text;
namespace LeetCode.Problem1576{
//1576. Replace All ?'s to Avoid Consecutive Repeating Characters
//https://leetcode.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.
*/
public class Solution {
public string ToLowerCase(string s) {
StringBuilder ans = new StringBuilder();
foreach (var item in s)
{
if (item >= 65 && item <= 90)
ans.Append((char)(item + 32));
else ans.Append(item);
}
return ans.ToString();
}
public string ModifyString(string s) {
StringBuilder ans = new StringBuilder();
var hz = ' ';
int val = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '?')
{
char prev = i > 0 ? ans[i - 1] : ' ';
char next = i < s.Length-1 ? s[i + 1] : ' ';
val = 0;
do
{
hz = Convert.ToChar(97+val);
val++;
}
while(hz == prev || hz == next);
ans.Append(hz);
}
else
ans.Append(s[i]);
}
return ans.ToString();
}
}
}