C# program to print all sublists of a list

A sublist (or subsequence) of a string contains characters from the original string in the same order, but not necessarily consecutive. For example, from the string "xyz", possible sublists include "x", "xy", "xz", "y", "yz", "z", and "xyz".

This program generates all possible sublists of a given string by building them incrementally using nested loops and dynamic list operations.

How the Algorithm Works

The algorithm processes each character of the input string and creates new sublists by combining existing sublists with the current character. It maintains a list that grows with each iteration, containing all possible sublists formed so far.

Sublists Generation Process Input: "xyz" Step 1: Add "x" Step 2: Add "xy" Step 3: Add "xyz" Final: x, xy, xyz, xz, y, yz, z

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main(string[] args) {
      string str = "xyz";
      List<string> list = new List<string>();
      
      for (int i = 1; i < str.Length; i++) {
         list.Add(str[i - 1].ToString());
         List<string> newlist = new List<string>();
         
         for (int j = 0; j < list.Count; j++) {
            string list2 = list[j] + str[i];
            newlist.Add(list2);
         }
         list.AddRange(newlist);
      }
      
      list.Add(str[str.Length - 1].ToString());
      list.Sort();
      
      Console.WriteLine("All sublists of "" + str + "":");
      foreach (string sublist in list) {
         Console.WriteLine(sublist);
      }
   }
}

The output of the above code is −

All sublists of "xyz":
x
xy
xyz
xz
y
yz
z

Using Recursive Approach

An alternative recursive approach to generate sublists involves choosing whether to include or exclude each character −

using System;
using System.Collections.Generic;

class Program {
   static void GenerateSublists(string str, int index, string current, List<string> result) {
      if (index == str.Length) {
         if (current.Length > 0) {
            result.Add(current);
         }
         return;
      }
      
      // Exclude current character
      GenerateSublists(str, index + 1, current, result);
      
      // Include current character
      GenerateSublists(str, index + 1, current + str[index], result);
   }
   
   static void Main(string[] args) {
      string str = "abc";
      List<string> sublists = new List<string>();
      
      GenerateSublists(str, 0, "", sublists);
      sublists.Sort();
      
      Console.WriteLine("All sublists of "" + str + "":");
      foreach (string sublist in sublists) {
         Console.WriteLine(sublist);
      }
   }
}

The output of the above code is −

All sublists of "abc":
a
ab
abc
ac
b
bc
c

Key Points

  • The iterative approach builds sublists by extending existing ones with each new character.

  • The recursive approach explores all possibilities of including or excluding each character.

  • Both methods generate 2n - 1 sublists for a string of length n (excluding the empty string).

  • The Sort() method arranges the sublists in lexicographical order for better readability.

Conclusion

Generating sublists of a string can be accomplished using both iterative and recursive approaches. The iterative method builds sublists incrementally, while the recursive method explores all character inclusion possibilities, both producing the same comprehensive set of sublists.

Updated on: 2026-03-17T07:04:35+05:30

748 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements