← Home
For problem statement at 1000-1999/1400-1499/1490-1499/1493/problemC.txt this is a correct solution, but verifier at 1000-1999/1400-1499/1490-1499/1493/verifierC.go ends with All 100 tests passed. can you fix the verifier? package main

import (
	"bufio"
	"bytes"
	"io"
	"os"
)

func main() {
	data, _ := io.ReadAll(os.Stdin)
	idx := 0

	nextInt := func() int {
		for idx < len(data) && (data[idx] == ' ' || data[idx] == '\n' || data[idx] == '\r' || data[idx] == '\t') {
			idx++
		}
		val := 0
		for idx < len(data) && data[idx] >= '0' && data[idx] <= '9' {
			val = val*10 + int(data[idx]-'0')
			idx++
		}
		return val
	}

	nextString := func() string {
		for idx < len(data) && (data[idx] == ' ' || data[idx] == '\n' || data[idx] == '\r' || data[idx] == '\t') {
			idx++
		}
		start := idx
		for idx < len(data) && data[idx] != ' ' && data[idx] != '\n' && data[idx] != '\r' && data[idx] != '\t' {
			idx++
		}
		return string(data[start:idx])
	}

	t := nextInt()
	var out bytes.Buffer

	for ; t > 0; t-- {
		n := nextInt()
		k := nextInt()
		s := nextString()

		if n%k != 0 {
			out.WriteString("-1\n")
			continue
		}

		b := []byte(s)
		cnt := [26]int{}
		for i := 0; i < n; i++ {
			cnt[b[i]-'a']++
		}

		ok := true
		for i := 0; i < 26; i++ {
			if cnt[i]%k != 0 {
				ok = false
				break
			}
		}
		if ok {
			out.WriteString(s)
			out.WriteByte('\n')
			continue
		}

		ans := ""
		for i := n - 1; i >= 0 && ans == ""; i-- {
			cnt[b[i]-'a']--
			rem := n - i - 1

			for c := int(b[i]-'a') + 1; c < 26; c++ {
				cnt[c]++

				needSum := 0
				needArr := [26]int{}
				for j := 0; j < 26; j++ {
					need := (k - cnt[j]%k) % k
					needArr[j] = need
					needSum += need
				}

				if needSum <= rem && (rem-needSum)%k == 0 {
					extra := rem - needSum
					res := make([]byte, 0, n)
					res = append(res, b[:i]...)
					res = append(res, byte('a'+c))
					for x := 0; x < extra; x++ {
						res = append(res, 'a')
					}
					for j := 0; j < 26; j++ {
						for x := 0; x < needArr[j]; x++ {
							res = append(res, byte('a'+j))
						}
					}
					ans = string(res)
					break
				}

				cnt[c]--
			}
		}

		if ans == "" {
			out.WriteString("-1\n")
		} else {
			out.WriteString(ans)
			out.WriteByte('\n')
		}
	}

	w := bufio.NewWriter(os.Stdout)
	w.Write(out.Bytes())
	w.Flush()
}