Leetcode Note: Go - DI String Match

DI String Match - LeetCode
https://leetcode.com/problems/di-string-match/

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

回答

DI String Match - LeetCode
https://leetcode.com/problems/di-string-match/solution/

Simple Explanation and solution with go - LeetCode Discuss
https://leetcode.com/problems/di-string-match/discuss/1866525/Simple-Explanation-and-solution-with-go

func diStringMatch(s string) []int {
	result := make([]int, len(s)+1)
	low, high := 0, len(s)

	for i, v := range s {
		if v == 'I' {
			result[i] = low
			low++
		} else {
			result[i] = high
			high--
		}
	}
	result[len(s)] = low

	return result
}