Golang Program to Count The Possible Decodings of a Given Digit Sequence


A digit sequence in go language is a set of digits that is used to represent a number. We can represent the digit sequence by using go languages existing data type.

In this article, the Golang program is  designed to calculate the possible decodings of a given digit sequence. It solves this problem by using dynamic programming techniques. Given a sequence of numbers, the program calculates the number of ways numbers can be determined.Here we are going to use the method countDecodings along with examples to elaborate the concept.

Syntax

func countDecodings(digits string) int

The countDecodings function is expected to take a string of digits as input and return an integer value, likely representing the count of possible decodings for that digit sequence.

Algorithm

  • Define a function to count the possible decodings, taking the digit sequence as input.

  • If the digit sequence is empty or has only one digit, return 1 as there is only one possible decoding.

  • Create a dynamic programming array of the same length as the digit sequence to store the counts of possible decodings at each position.

  • Initialize the first two elements of the dynamic programming array as 1, indicating that there is one possible decoding for the first two digits.

  • Iterate through the digit sequence starting from the third digit.

  • For each digit, check if it can be combined with the previous digit to form a valid decoding. If so, add the count of decodings at the previous position to the count at the current position in the dynamic programming array.

  • Finally, return the count of decodings at the last position in the dynamic programming array, which represents the total number of possible decodings for the given digit sequence.

Example

In the countDecodings function, we first handle the base cases. If the digit sequence is empty, we return 1 to indicate that there is one possible decoding (an empty string). If the first digit is '0', it means there is no possible decoding, so we return 0.Finally, in the main function, we define a digit sequence, call the countDecodings method with the sequence, and print the resulting count of possible decodings.

package main

import "fmt"

func countDecodings(digits string) int {
   if len(digits) == 0 {
      return 1
   }

   if digits[0] == '0' {
      return 0
   }

   count := 0

   count += countDecodings(digits[1:])

   if len(digits) >= 2 {
      num := int(digits[0]-'0')*10 + int(digits[1]-'0')
      if num <= 26 {
         count += countDecodings(digits[2:])
      }
   }

   return count
}

func main() {
   digits := "123"
   result := countDecodings(digits)
   fmt.Printf("Number of possible decodings: %d\n", result)
}

Output

Number of possible decodings: 3

Conclusion

In this article we discussed the Golang program which provides a solution for calculating the possible decodings of a given digit sequence. Using the dynamic programming method, it effectively calculates the number by deciding one number and two decision numbers.

Updated on: 20-Jul-2023

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements