All Possible Unique Substrings From an Input String

A program that will get an input string and provide the possible unique substrings of that string.

Ususally when a programmer goes to the interview, the company asks to write the code of some program to check your logic and coding abilities.

You can write the code in any language like C, C++, Java, C# and so on. There is no barrier to the language and technology because the company wants to check your ability to think and your skills to write the code.

Some companies only want the algorithm to check your skills.

One of the programs in those common programs is the following.

Write a function that will take a string A as input and print all the possible unique substrings present in a given string A.

Example

Input = ”abcde”
Output = a, ab, abc, abcd, abcde, b, bc, bcd, bcde, c, cd, cde, d, de, e

Input = ”hello”
Output = h, he, hel, hell, hello, e, el, ell, ello, l, ll, llo, lo, o

Input = ” abab”
Output = a, ab, aba, abab, b, ba, bab

To do this, I will use the Visual Studio 2012 as an IDE and C# as a language,

Create a new console application named "ConsoleApplication1".



Where you will get the page that will look like:



Now I will write the code in some steps.

Get the input string from the user.

  1. Console.Write("Eneter the input String:");        
  2. //Get the string from the user and save it in a variable  
  3. string input = Console.ReadLine();  


Write the code in a function to get all the possible unique substrings from the input string.
  1. public static void PossibleUniqueSubString(string input)  
  2. {  
  3.     //declare the string variable  
  4.     string final = "";  
  5.   
  6.     //Get a string of possible substrings with out uniqueness  
  7.     for (int i = 0; i < input.Length; i++)  
  8.     {  
  9.         string str = "";  
  10.         for (int j = i; j < input.Length; j++)  
  11.         {  
  12.             str += input[j];  
  13.             final += str + ",";  
  14.         }  
  15.     }  
  16.   
  17.     //Remove the ending comma from the string  
  18.     final = final.Remove(final.Length - 1, 1);  
  19.   
  20.     //Get an array after Spilt the string on basis of comma  
  21.     string[] arr = final.Split(',');  
  22.   
  23.     //Get the distinct array  
  24.     arr = arr.Distinct().ToArray();  
  25.   
  26.     //Print the array  
  27.     for (int i = 0; i < arr.Length; i++)  
  28.         Console.Write(arr[i]+" ");  
  29.  }  
Call the function with the parameter of input string in the main() method as in the following:
  1. Console.Write("Eneter the input String:");  
  2.   
  3. //Get the string from the user and save it in a variable  
  4. string input = Console.ReadLine();   
  5. PossibleUniqueSubString(input);  
If I provide the ”abcde” as an input string then the output will be like:

a, ab, abc, abcd, abcde, b, bc, bcd, bcde, c, cd, cde, d, de, e



If I provide ”hello” as an input string then the output will be like:

h, he, hel, hell, hello, e, el, ell, ello, l, ll, llo, lo, o



If I provide ”abab” as an input string then the output will be like:

a, ab, aba, abab, b, ba, bab



Similar Articles