1576. 替换所有的问号
替换指定字符串中的’?’并且使不能有连续一样的字符
遍历的检测到’?’就检测前后位置替换字符
public static String modifyString(String s) {
StringBuilder sb = new StringBuilder();
char[] ss = s.toCharArray();
if (ss.length==1){
if(ss[0]=='?'){
return "a";
}
else return s;
}
for (int i = 0; i < ss.length; i++) {
if (ss[i]=='?'){
if (i != 0 && i != ss.length - 1) {
char a = (char) (randomNumber(25) + 97);
while(a==ss[i-1]||ss[i+1]==a) {
a = (char) (randomNumber(25) + 97);
}
ss[i] = a;
} else if (i==0){
char a = (char) (randomNumber(25) + 97);
while(ss[i+1]==a) {
a = (char) (randomNumber(25) + 97);
}
ss[i] = a;
} else if (i == ss.length - 1) {
char a = (char) (randomNumber(25) + 97);
while (a == ss[i - 1]) {
a = (char) (randomNumber(25) + 97);
}
ss[i] = a;
}
}
sb.append(ss[i]);
}
return sb.toString();
}
public static int randomNumber(int bound) {
return (int) (Math.random() * 100000) % bound;
}