# converting a program into dynamic

Page 1 of 1

## 5 Replies - 384 Views - Last Post: 01 August 2013 - 03:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=326013&amp;s=9600d2ed675a2b725f9286782257cbf6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 NitinJotwani

Reputation: 0
• 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.)
*/
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

• Cabbage

Reputation: 2244
• Posts: 4,722
• 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.

### #3 NitinJotwani

Reputation: 0
• 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!!

### #4 cfoley

• Cabbage

Reputation: 2244
• Posts: 4,722
• 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.

### #5 NitinJotwani

Reputation: 0
• 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

### #6 pbl

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

Reputation: 8365
• Posts: 31,956
• 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>();
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);
}
String item = str.concat(first.toString());
}
return result;
}

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

}
}

```