Skip to content

Print All subssequence of the string

  • Subsequence is not substring
  • subsequence string can be generated by selecting charactor from the given string
  • It should maintain the order
  • Valid subsequence: abc and acd are subsequence of abcd
  • Invalid subsequence: acb and dac is not subsequence of abcd because order is not maintained

Ex1:
Input: abcd
Outout: [abcd abc abd ab acd ac ad a bcd bc bd b cd c d ]

package main

import "fmt"

var arr []string

func main() {
    arr = make([]string, 0)
    str := "abcd"
    subseq(str, "")
    fmt.Println(arr)
}

func subseq(str, ans string) {
    if len(str) == 0 {
        arr = append(arr, ans)
        return
    }
    firstChar := string([]rune(str)[0])
    subseq(str[1:], ans+firstChar)
    subseq(str[1:], ans)

}