Thursday, 2 December 2021

Program to find all subsets of a string

pallindrome

Program to find all subsets of a string

In this program, all the subsets of the string need to be printed. The subset of a string is the character or the group of characters that are present inside the string. All the possible subsets for a string will be n(n+1)/2.

For example, all possible subsets of a string "FUN" will be F, U, N, FU, UN, FUN. To complete this program, we need to run two for loops. The outer loop is used to maintain the relative position of the first character and the second loop is used to create all possible subsets and prints them one by one.

string 1

"FUN"

Program

class Q5_subset {

  public static void main(String[] args)   {

    //declare string

   String str1="FUN";

   int len = str.length();

   int temp=0;

   //Total possible subsets for string of size n is n*(n+1)/2

   String arr[] = new String[len*(len+1)/2];

  for(int i = 0; i < len; i++) {

  //This loop adds the next character every iteration for the subset to form and add it to the array

  for for(int j = i; j < len; j++) {

  arr[temp] = str.substring(i, j+1);

  temp++;

   }

  }

  //This loop prints all the subsets formed from the string.

  System.out.println( "All subsets for given string are: " );

  for(int i = 0; i < len; i++) {

  System.out.println(arr[i]);

     }

    }

   }

output

All subsets for given string are: F FU FUN U UN N

Theory

in this program , we need to check whether a string is palindrome or not. A string is said to palindrome then string backword write is some to write forword otherwise it is not a palindrome string

we are to travel loop to midle of the string to compare the first char to last char then increase the value of i an use flag is boolean varible declare is true but after travelling loop flag is rename true it is palindrome string as basis of our programming method other wise you are use various method as ssson possible to you.

No comments:

Post a Comment

How do you select data from a table in SQL?

Create Table How do you select data a table in SQL? The SELECT statement is used to se...