converting a program into dynamic

Page 1 of 1

5 Replies - 406 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=262f0d10e684926581e73b390ca60f62&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 NitinJotwani

• New D.I.C Head

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.)
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

• Cabbage

Reputation: 2338
• Posts: 4,889
• 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

• New D.I.C Head

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!!
Was This Post Helpful? 0

#4 cfoley

• Cabbage

Reputation: 2338
• Posts: 4,889
• 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

• New D.I.C Head

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

Was This Post Helpful? 0

#6 pbl

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

Reputation: 8370
• 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>();
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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}