Leetcode Note: Go - Replace All ?'s to Avoid Consecutive Repeating Characters

Replace All ?’s to Avoid Consecutive Repeating Characters - LeetCode
https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/

  • Go 言語で取り組んだメモ

回答

Golang Simple O(n) - Replace All ?’s to Avoid Consecutive Repeating Characters - LeetCode
https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/solutions/1565630/golang-simple-o-n/

func modifyString(s string) string {
    str := strings.Split(s, "")
    chars := []string{"a", "b", "c"}
	
    for i := range str {
        if str[i] != "?" {
			continue
		}
		
		for _, c := range chars {
			if (i == 0 || str[i-1] != c) && (i == len(s)-1 || str[i+1] != c) {
				str[i] = c
				break
			}
		}
    }
	
    return strings.Join(str[:], "")
}