Leetcode Note: Go - Find Smallest Letter Greater Than Target

Find Smallest Letter Greater Than Target - LeetCode
https://leetcode.com/problems/find-smallest-letter-greater-than-target/

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

所感

  • byte 配列 letters と byte 型の target が渡される
  • letters の中で target よりも最も小さい byte を return する

回答

Find Smallest Letter Greater Than Target - LeetCode
https://leetcode.com/problems/find-smallest-letter-greater-than-target/solution/

Go ⭐ - LeetCode Discuss
https://leetcode.com/problems/find-smallest-letter-greater-than-target/discuss/2389025/Go

func nextGreatestLetter(letters []byte, target byte) byte {
	var lettersList = make([]int, 26)

	for _, letter := range letters {
		lettersList[letter-'a'] = 1
	}

	for i, letter := range lettersList {
		if letter == 1 && byte(i)+'a' > target {
			return byte(i) + 'a'
		}
	}

	for i, letter := range lettersList {
		if letter == 1 {
			return byte(i) + 'a'
		}
	}

	return 0
}