Wednesday, 6 June 2018

Find out biggest substring from given string having all distinct character

 using System;  
 namespace AlgoApp  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.WriteLine("Press any key to continue or n to discontinue");  
       //while (Console.ReadLine() != "n")  
       //{  
       Console.WriteLine("input string!");  
       String inputStr = Console.ReadLine();  
       String str = inputStr.Substring(0, 1);  
       String str2 = string.Empty;  
       String strFinal = string.Empty;  
       if (inputStr.Length > 1)  
       {  
         //abcd  
         //abbccde  
         //aaaa  
         //abcade  
         //abbcde  
         //abbccdefffefghi  
         int count = 0;  
       foreach (char c in inputStr)  
       {  
         for (int i = count; i < inputStr.Length-1; i++)  
         {  
           if (!IsDuplicate(str, inputStr.Substring(i + 1, 1)))  
           {  
             str = str + inputStr.Substring(i + 1, 1);  
           }  
           else  
           {  
               if (strFinal.Length < str.Length)  
                 strFinal = str;  
             str = string.Empty;  
             break;  
           }  
         }  
         if (strFinal.Length < str.Length)  
         {  
           strFinal = str;  
         }  
         count = count + 1;  
       }  
         Console.WriteLine("substring having distinct character= " + strFinal);  
       }  
         else if (inputStr.Length == 1)  
         {  
           Console.WriteLine("substring having distinct character= " + inputStr);  
         }  
         else  
         {  
           Console.WriteLine("input string should be in lowercase alphabat letter");  
         }  
         Console.WriteLine("Press any key to continue or n to discontinue");  
       // }  
       Console.ReadLine();  
     }  
     public static bool IsDuplicate(string str,string c)  
     {  
       bool isDuplicateChar = false;  
       for(int i=0;i<str.Length;i++)  
       {  
         if(str.Substring(i,1)==c)  
         {  
           isDuplicateChar = true;  
         }  
       }  
       return isDuplicateChar;  
     }  
   }  
 }  

No comments:

Post a Comment