How To Find All Possible Subsets of a String Using C#

Introduction

In this article, we will explore a method to find all possible subsets of a string in C#.

Below are a few examples of inputs and Outputs.

Examples

  1. Input: ABC
  2. Output: A AB ABC BC C
  3. Input: RUN
  4. Output: R RU RUN U UN N

This is an essential technical interview question that may be posed to beginner, intermediate, and experienced candidates.

We previously covered the following.

  1. How to Reverse Number in C#
  2. How to Reverse a String in C#
  3. Palindrome String Program in C#
  4. Palindrome Number in C#
  5. How to Reverse Order of the Given String
  6. How To Reverse Each Word Of Given String
  7. How To Remove Duplicate Characters From String In C#
  8. Bubble Sort Algorithm
  9. How To Count the Occurrence Of Each Character In The String In C#
  10. Decimal to Binary Conversion in C#
  11. Binary To Decimal Conversion in C#

Let’s start,

using System.Text;

Console.WriteLine("Enter a string");
var str = Console.ReadLine();

for (int i = 0; i < str.Length; i++)
{
    StringBuilder sb = new StringBuilder(str.Length - i);
    for (int j = i; j < str.Length; j++)
    {
        sb.Append(str[j]);
        Console.Write(sb + " ");
    }
}

Console.ReadLine();

Let us execute the program and see the output.

Output

Output

Try with another input.

Input

We have learned a method to find all possible subsets of a string in C#. I hope you find this article enjoyable and useful.


Similar Articles