
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program To Find all the Subsets of a String
In this article, we will understand how to find all the subsets of a string. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). A part or a subset of string is called substring.
Below is a demonstration of the same −
Suppose our input is −
The string is defined as: JVM
The desired output would be −
The subsets of the string are: J JV JVM V VM M
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Initialize a temporary variable to increment after every iteration. Step 5 - Iterate through the length of the string using two nested loops. Step 6 - Find substring between a given range, and increment the temporary variable after every iteration. Step 7 - Display the substrings using a loop. Step 8 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class Demo { public static void main(String[] args) { String input_string = "JVM"; int string_length = input_string.length(); int temp = 0; System.out.println("The string is defined as: " +input_string); String string_array[] = new String[string_length*(string_length+1)/2]; for(int i = 0; i < string_length; i++) { for(int j = i; j < string_length; j++) { string_array[temp] = input_string.substring(i, j+1); temp++; } } System.out.println("The subsets of the string are: "); for(int i = 0; i < string_array.length; i++) { System.out.println(string_array[i]); } } }
Output
The string is defined as: JVM The subsets of the string are: J JV JVM V VM M
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class Demo { static void subsets(String input_string){ int string_length = input_string.length(); int temp = 0; String string_array[] = new String[string_length*(string_length+1)/2]; for(int i = 0; i < string_length; i++) { for(int j = i; j < string_length; j++) { string_array[temp] = input_string.substring(i, j+1); temp++; } } System.out.println("The subsets of the string are: "); for(int i = 0; i < string_array.length; i++) { System.out.println(string_array[i]); } } public static void main(String[] args) { String input_string = "JVM"; System.out.println("The string is defined as: " +input_string); subsets(input_string); } }
Output
The string is defined as: JVM The subsets of the string are: J JV JVM V VM M
- Related Articles
- Golang program to find all subsets of a string
- Java program to find all duplicate characters in a string
- How to find all subsets of a set in JavaScript?
- Python program to get all subsets of given size of a set
- Python program to get all subsets of a given size of a set
- Java program to find all close matches of input string from a list
- Find all distinct subsets of a given set in C++
- Java Program to Print all unique words of a String
- C++ Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
- C++ Program to Generate All Pairs of Subsets Whose Union Make the Set
- Java Program to count all vowels in a string
- Java Program to Remove All Whitespaces from a String
- Java Program to find all angles of a triangle
- Python program to get all subsets having sum s\n
- List all the subsets of a set {m , n}

Advertisements