方法一 3個字母原則
把?替換為和他左右都不相等的字符,那么找3個字符abc,?總能替換為abc中的一個字符,遍歷字符串找到所有?,再遍歷abc把?替換為abc中的一個字符
var modifyString = function(s) {const chars = 'abc';let res = '';for(let i=0; i<s.length; i++) {if(s[i] === '?') {let index = 0;while(chars[index] === res[i-1] || chars[index] === s[i+1]){index++}res += chars[index];}else{res += s[i];}}return res;
};
?消耗時間和內存情況: