Leetcode Note: Go - Largest 3 Same Digit Number in String

Largest 3-Same-Digit Number in String - LeetCode
https://leetcode.com/problems/largest-3-same-digit-number-in-string/

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

回答

✅2264. Largest 3-Same-Digit Number in String - Largest 3-Same-Digit Number in String - LeetCode
https://leetcode.com/problems/largest-3-same-digit-number-in-string/solutions/3408679/2264-largest-3-same-digit-number-in-string/

func largestGoodInteger(num string) string {
	tmp, cnt, n := num[0], 0, []int{}
	for i := 1; i < len(num); i++ {
		if tmp == num[i] {
			cnt += 1
		} else {
			cnt = 0
		}
		if cnt == 2 {
			t := string(tmp) + string(tmp) + string(tmp)
			tt, _ := strconv.Atoi(t)
			n = append(n, tt)
			cnt = 0
		}
		tmp = num[i]
	}
	if len(n) == 0 {
		return ""
	}
	sort.Ints(n)
	ans := strconv.Itoa(n[len(n)-1])
	if ans == "0" {
		return "000"
	}
	return ans
}