5 Replies - 279 Views - Last Post: 01 August 2013 - 03:55 AM Rate Topic: -----

#1 NitinJotwani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 28-September 12

converting a program into dynamic

Posted 31 July 2013 - 07:02 AM

/*
Write a program to generate all strings of length upto k on a given set of symbols.     
(Hint: I/p:Take 6 symbols in the lower-case English alphabet {a, b, c, d, e, f} and k = 3; print the first 25 strings of your output sorted by length.)
O/p: a,b,c,d,e,f,aa,ab,ac,ad,ae,af, ba, bb…
*/
class TestDict
{
     public static void main(String Nitin[])
     {
          char arr[]={'a','b','c','d','e','f'};
          int max=3;
          //combination of 1
          for(int i=0;i<arr.length;i++)
          {
               System.out.print(arr[i]+"  ");
          }
          
          //combination of 2
          for(int i=0;i<arr.length;i++)
          {
               for(int j=0;j<arr.length;j++)
               {
                    System.out.print(arr[i]+""+arr[j]+"  ");
               }
          }
          
          //combination of 3
          for(int i=0;i<arr.length;i++)
          {
               for(int j=0;j<arr.length;j++)
               {
                     for(int k=0;k<arr.length;k++)
                     {
                          System.out.print(arr[i]+""+arr[j]+""+arr[k]+"  ");
                     }
               }
          }
    }
}



no doubt, it works fine, but what if i had to go for combination of chars of length till 8 or 9...so i cant write loop every time for each combination, how can i overcome this???

Is This A Good Question/Topic? 0
  • +

Replies To: converting a program into dynamic

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: converting a program into dynamic

Posted 31 July 2013 - 07:09 AM

Wrap another for loop that goes from 1 up to 8 or 9. Use it to control the length of sequence generated.
Was This Post Helpful? 0
  • +
  • -

#3 NitinJotwani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 28-September 12

Re: converting a program into dynamic

Posted 31 July 2013 - 07:28 AM

can u give example, i tried before but didnt come up!!
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: converting a program into dynamic

Posted 31 July 2013 - 08:36 AM

Post your attempt and we can try and work out where you went wrong.
Was This Post Helpful? 0
  • +
  • -

#5 NitinJotwani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 28-September 12

Re: converting a program into dynamic

Posted 31 July 2013 - 07:14 PM

class TestDict
{
     public static void main(String Nitin[])
     {
          char arr[]={'a','b','c','d','e','f'};
          int max=3;
          for(int m=1;m<=max;m++)
          {
          for(int i=0;i<arr.length;i++)
          {
               for(int h=0;h<m;h++)
                   System.out.print(arr[i]+"  ");
          }
          }
      }
}


output:a b c d e f a a b b c c d d e e f f a a a b b b c c c d d d e e e f f f

works for same chars, thts the problem,

This post has been edited by NitinJotwani: 31 July 2013 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: converting a program into dynamic

Posted 01 August 2013 - 03:55 AM

You don't need the ""

 System.out.print(arr[i]+""+arr[j]+""+arr[k]+"  ");


and
 System.out.print(arr[i]+arr[j]+arr[k]+"  ");


are equivalent

Should be like that
import java.util.ArrayList;  
import java.util.List;  

public class StringPermuation {  

	public static ArrayList<String> getPermutation(String input) {  

		ArrayList<String> collection = null;  

		if (input.length() == 1) {  
			collection = new ArrayList<String>();  
			collection.add(input);  
			return collection;  
		}   
		
		collection = getPermutation(input.substring(1));  
		Character first = input.charAt(0);  
		ArrayList<String> result = new ArrayList<String>();  
		for (String str : collection) {  
			for (int i = 0; i < str.length(); i++) {  
				String item = str.substring(0, i) + first  
						+ str.substring(i);  
				result.add(item);  
			}  
			String item = str.concat(first.toString());  
			result.add(item);  
		}  
		return result;  
	}  


	public static void main(String[] args) {  
		System.out.println(getPermutation("abcd"));  

	}  
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1