BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to find all subsets of a string

By Chaitanya Singh | Filed Under: Java Examples

In this article, you will learn how to write a java program to find all subsets of a String. If the number of characters in a given string is n then the number of possible subsets of that string would be: n(n+1)/2. For example: if the length of a string is 4 then the number of subsets would be: 4(4+1)/2 = 10.

A String “TOY” has 3(3+1)/2 = 6 subset strings: “T”, “O”, “Y”, “TO”, “OY”, “TOY”.

Java Program to Find all subsets of a String

Steps followed in the example are:

  1. Find the length of the given string using length() method.
  2. Create a String array to hold all the subsets of the given string, size of the array would be n(n+1)/2.
  3. Inside nested for loop use substring() method to find all the possible subsets of the string. Store them in the array created in the step 2.
  4. loop the array and print all the elements to display all the subsets.
public class JavaExample {
  public static void main(String[] args) {

    String str = "TEXT";
    //finding length of the given string
    int numberOfChar = str.length();
    int temp = 0;
    //If number of characters in a string is n then the
    //possible subsets of that string is n*(n+1)/2
    String subsetArray[] = new String[numberOfChar*(numberOfChar+1)/2];

    for(int i = 0; i < numberOfChar; i++) {
      for(int j = i; j < numberOfChar; j++) {
        subsetArray[temp] = str.substring(i, j+1);
        temp++;
      }
    }

    //Print all the subsets of the given string
    System.out.println("All the possible subsets of the given string: ");
    for(int i = 0; i < subsetArray.length; i++) {
      System.out.println(subsetArray[i]);
    }
  }
}

Output:
Java Program to find all subsets of a string

Related Java Examples:

  1. Java Program to divide a String in n equal parts
  2. Java Program to check two strings are anagram or not
  3. Java Program to compare two strings
  4. Java Program to count vowels and consonants in a String
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap