Skip to content

Latest commit

 

History

History
24 lines (22 loc) · 680 Bytes

1576.md

File metadata and controls

24 lines (22 loc) · 680 Bytes

1576. Replace All ?'s to Avoid Consecutive Repeating Characters

Solution 1 (time O(n), space O(1))

class Solution(object):
    def modifyString(self, s):
        """
        :type s: str
        :rtype: str
        """
        c = set("abcdefghijklmnopqrstuvwxyz")
        new_s = list(s)
        for i in range(len(new_s)):
            if new_s[i] != "?":
                continue
            cur_set = []
            if i != 0:
                cur_set.append(new_s[i - 1])
            if i != len(new_s) - 1:
                cur_set.append(new_s[i + 1])
            new_s[i] = list(c - set(cur_set))[0]
        return "".join(new_s)