Array-promo

Question

Find all the subsets of a word

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class SubsetWord 
{  
     
    public static void main(String[] args)  
    {  
        String wd="";
        String arr[];
        int temp=0,len=0,i=0,j=0;
        Scanner sc = new Scanner(System.in);    

        System.out.println("Enter a word");   
        
        wd =sc.next(); 
        len = wd.length();  
        temp = 0;  
        /*Total possible subsets for string of size len is len*(len+1)/2  */
        arr = new String[len*(len+1)/2];  
          
          
        for(i = 0; i < len; i++) 
        {  
            /*loop adds the next character every iteration for the subset to form and add it to the array */ 
            for(j = i; j < len; j++)
            {  
                arr[temp] = str.substring(i, j+1);  
                temp++;  
            }  
        }  
          
        /*prints all the subsets  from the variable 'arr'*/  
        System.out.println("subsets for "+wd+ " are: ");  
        for(int i = 0; i < arr.length; i++) {  
            System.out.println(arr[i]);  
        }  
    }  
      
   
}  

				
			

Coding Store

Leave a Reply

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