Golang program to find all subsets of a string


A string in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. A built-in type in Go, the string type can be used in a variety of ways much like any other data type.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and sort package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a string and print it on the console using print statement in Golang.

  • Step 3 − Call the function subset_string from the console with the string as an input inside it.

  • Step 4 − In the function a variable named size is first initialized to the length of the input string.

  • Step 5 − After then, all integers between 0 and 2(size)-1 are iterated over using an outer loop, where each bit of an integer corresponds to a character in the input string.

  • Step 6 − It examines the jth bit of the current integer I in the inner loop. If the jth bit is set to one (1), the input string's jth character is a part of the current subset.

  • Step 7 − The character is then added to the subset, and the subset is subsequently printed.

  • Step 8 − Up until every subset of the input string has been identified and printed, the outer loop continues.

  • Step 9 − The print statement is executed using fmt.Println() function where ln means new line.

  • Step 10 − This algorithm uses the idea of bit manipulation to generate all possible subsets of the input string by iterating over all integers from 0 to 2^size-1 and checking each bit to determine which characters to include in the current subset.

Example 1

In this example, we will grasp how to use bit manipulation approach to get all subsets of a string.

package main
import "fmt"

func subset_string(string_val string) {
   size := len(string_val) //denotes length of input string
   for i := 0; i < (1 << uint(size)); i++ {
      subset := ""
      for j := 0; j < size; j++ {
         if i&(1<<uint(j)) != 0 {
            subset += string(string_val[j])
         }
      }
      fmt.Print(subset + " ") //subset is subsequently printed
   }
}
func main() {
   string_val := "rit" //Create a string
   fmt.Println("The initial string created here is:", string_val)
   fmt.Println("The subsets of the string created is:")
   subset_string(string_val)
}

Output

The initial string created here is: rit
The subsets of the string created is:
 r i ri t rt it rit 

Example 2

This example employs the recursive function called subsets.

package main
import "fmt"

func subsets(mystr string, start int, subset string) {
   fmt.Println(subset)
   for i := start; i < len(mystr); i++ { //run a loop till the length of mystr
      subsets(mystr, i+1, subset+string(mystr[i]))
   }
}
func main() {
   mystr := "rit" //Create string
   fmt.Println("The original string created here is:", mystr)
   subsets(mystr, 0, "") //call the function subsets with three arguments
}

Output

The original string created here is: rit

r
ri
rit
rt
i
it
t

Conclusion

We executed the program of finding all subsets of a string using two examples. In the first example we used bit manipulation approach and in the second example we used recursive approach to execute the program. Both the programs gave similar output of subsets. Hence, program executed successfully.

Updated on: 13-Feb-2023

481 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements