top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

JAVA Program for all combinations of (n-1) length substring of n length string ?

+4 votes
855 views

For example:
If string is "abcd" the output should "abc", "abd", "acd", "bcd".

posted Nov 4, 2014 by Sudhakar Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
if a string is of n length then what we need is a string with n-1 characters which will have nC(n-1) ways or n ways. Just traverse the string say from index 0 to (n-1) and pick the string other then index character. Hope it helps.

1 Answer

+2 votes

Try something like this based on the logic which Salil suggested. (code is not tested)

import java.util.Scanner;

class SubstringsOfAString
{
   public static void main(String args[])
   {
      String string, sub, sub1;
      int i, c, length;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to print it's all substrings");
      string  = in.nextLine();

      length = string.length();   

      System.out.println("Substrings of \""+string+"\" are :");

      for( c = 0 ; c < length ; c++ )
      {
          if (c==0)
          {
            sub = string.substring(c+1, length-1);
          }
          else // we need two string 0 to index-1 and index+1 to end then concat 
          {
            sub = string.substring(0, c -1); 
            sub1 = string.substring(c+1, length-1); 
            System.out.println(sub.concat(sub1));
          }
      }
   }
}
answer Nov 4, 2014 by Akriti
here is my logic
class stringtest
{
    public static void main(String args[])
    {
        String str = new String("abcd");
        String str1, temp;
        /*for(int i=(str.length()-1); i>=0; i--)
        {
            str1 = str.replace(str.charAt(i), '\0');
            System.out.println("using replace:"+ str1);
        }*/

        for(int i=(str.length()-1); i>=0; i--)
        {
            temp = str.substring(i, i+1);
            str1 = str.replace(temp, "");
            System.out.println("using null string replace:"+ str1);
        }

        for(int i=(str.length()-1); i>=0; i--)
        {
            StringBuffer sb = new StringBuffer(str);
            sb.deleteCharAt(i);
            str1 = new String(sb);
            System.out.println("using delete:"+ str1);
        }
    }
}
Faster then above approach :)
Similar Questions
+1 vote

While reading possible time complexity i found that time complexity O(log log n) exist for some algos,

Can any one explain an example which shows the calculation of the time complexity with O(log log n)

+2 votes

convert a number m to n with minimum operations. The operations allowed were -1 and *2.

For Eg : 4 and 6. Answer is 2.
1st operation : -1 -> 4-1 = 3.
2nd operation : * -> 3 * 2 =6.

+4 votes

Calculate 1+2+…+n without multiplication, division, key words for, while, if, else, switch, case, as well as conditional operator (A ? B : C).

+3 votes

In an "N" element integer sorted array, a particular elements is repeating "(N/2)+1" times. How much time it take to find the repeating element.

...