Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Count Possible Decodings of a digit sequence into letters in Go (Golang)

Posted on January 31, 2023January 31, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

Let’s say we have below number to letter mapping

'A' -> "1"
'B' -> "2"
...
'Z' -> "26"

The objective is to count the number of possible decoding of a given digit sequence

Examples

Input: '15'
Output: 2
15 can be decoded as "AE" or "O"

Program

Here is the program for the same.

package main
import (
"fmt"
"strconv"
)
func numDecodings(s string) int {
runeArray := []rune(s)
length := len(runeArray)
if length == 0 {
return 0
}
if string(runeArray[0]) == "0" {
return 0
}
numwaysArray := make([]int, length)
numwaysArray[0] = 1
if length == 1 {
return numwaysArray[0]
}
secondDigit := string(runeArray[0:2])
num, _ := strconv.Atoi(secondDigit)
if num > 10 && num <= 19 {
numwaysArray[1] = 2
} else if num > 20 && num <= 26 {
numwaysArray[1] = 2
} else if num == 10 || num == 20 {
numwaysArray[1] = 1
} else if num%10 == 0 {
numwaysArray[1] = 0
} else {
numwaysArray[1] = 1
}
for i := 2; i < length; i++ {
firstDigit := string(runeArray[i])
if firstDigit != "0" {
numwaysArray[i] = numwaysArray[i] + numwaysArray[i-1]
}
secondDigit := string(runeArray[i-1 : i+1])
fmt.Println(i)
fmt.Println(secondDigit)
num, _ := strconv.Atoi(secondDigit)
if num >= 10 && num <= 26 {
numwaysArray[i] = numwaysArray[i] + numwaysArray[i-2]
}
}
return numwaysArray[length-1]
}
func main() {
output := numDecodings("15")
fmt.Println(output)
}

Output

2

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you -All Design Patterns Golang

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP