How Can I Use C# to Find Every Possible Subset of a String?

In this post, we’ll look at a C# technique for locating every feasible subset of a string. Here are some input and output examples.

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.

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

Try with another 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.

Best and Most Recommended ASP.NET Core 8.0.11 Hosting

Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 8.0.11 web ranking. HostForLIFE.eu is highly recommended. In Europe, HostForLIFE.eu is the most popular option for first-time web hosts searching for an affordable plan.

Their standard price begins at only €3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFE.eu guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.

By providing reseller hosting accounts, HostForLIFE.eu also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.