Skip to content

Latest commit

 

History

History
36 lines (29 loc) · 959 Bytes

1576. 替换所有的问号.md

File metadata and controls

36 lines (29 loc) · 959 Bytes
  • 模拟
function modifyString(s: string): string {

    const strs: string[] = [...s];

    strs.forEach((value: string, index: number, array: string[]) => {
        if (value !== '?') {
            return s;
        }
        const last = array[index - 1],
            next = array[index + 1];
        if (!last && (!next || next === '?')) {
            array[index] = 'a';
        } else if (!last) {
            array[index] = getNextChar(next);
        } else if (!next) {
            array[index] = getNextChar(last);
        } else {
            array[index] = getNextChar(last) !== next ? getNextChar(last) : getNextChar(next);
        }
    });

    return strs.join('');
};

function getNextChar(char: string): string {
    return String.fromCharCode(
        (char.charCodeAt(0) - 96) % 26 + 97
    );
}