423.reconstruct-original-digits-from-english.go 712 B

123456789101112131415161718192021222324252627282930313233
  1. func originalDigits(s string) string {
  2. freq, cnt := make([]int, 256), make([]int, 10)
  3. for _, r := range s {
  4. freq[r]++
  5. }
  6. cnt[0] = wordCnt(freq, 'z', "o")
  7. cnt[2] = wordCnt(freq, 'w', "o")
  8. cnt[4] = wordCnt(freq, 'u', "fo")
  9. cnt[6] = wordCnt(freq, 'x', "s")
  10. cnt[8] = wordCnt(freq, 'g', "h")
  11. //
  12. cnt[1] = wordCnt(freq, 'o', "n")
  13. cnt[3] = freq['h']
  14. cnt[5] = freq['f']
  15. cnt[7] = wordCnt(freq, 's', "n")
  16. //
  17. cnt[9] = freq['n'] / 2
  18. var sb strings.Builder
  19. for i := 0; i < 10; i++ {
  20. for j := 0; j < cnt[i]; j++ {
  21. sb.WriteByte(byte(i + '0'))
  22. }
  23. }
  24. return sb.String()
  25. }
  26. func wordCnt(freq []int, key byte, word string) int {
  27. cnt := freq[key]
  28. for _, r := range word {
  29. freq[r] -= cnt
  30. }
  31. return cnt
  32. }