Skip to content

Commit 5924a32

Browse files
SubString and SubArrays And Permutations of Stirngs
1 parent f621406 commit 5924a32

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

src/strings/SubSet.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package strings;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class SubSet {
7+
8+
//Normal fakt string cut karat jato
9+
static void subSeq(String temp, String str) {
10+
if (str.length() == 0) {
11+
System.out.println(temp);
12+
return;
13+
}
14+
subSeq(temp + str.charAt(0), str.substring(1));
15+
subSeq(temp, str.substring(1));
16+
}
17+
18+
// ARraylist return
19+
static ArrayList<String> subSeq2(String temp, String str,ArrayList< String> list) {
20+
if (str.length() == 0) {
21+
list.add(temp);
22+
return list ;
23+
}
24+
subSeq(temp + str.charAt(0), str.substring(1));
25+
subSeq(temp, str.substring(1));
26+
return list;
27+
}
28+
29+
30+
static List<List<Integer>> SubArray(int arr[])
31+
{
32+
List<List<Integer>> outer=new ArrayList<List<Integer>>();
33+
ArrayList<Integer> list=new ArrayList<>();
34+
outer.add(list);
35+
36+
// Each element of array should be added in the each element present inside the
37+
// previous list;
38+
39+
for(int num:arr)
40+
{
41+
// Taken Size here because we are adding elments in the loop
42+
int size=outer.size();
43+
44+
// Adding each element of array in previous lists.
45+
46+
for(int i=0;i<size;i++)
47+
{
48+
ArrayList<Integer> internal=new ArrayList<>();
49+
internal.addAll(outer.get(i));
50+
internal.add(num);
51+
outer.add(internal);
52+
}
53+
}
54+
55+
56+
return outer;
57+
58+
}
59+
60+
61+
static void permutations(String temp,String str)
62+
{
63+
if(str.length()==0)
64+
{
65+
System.out.println(temp);
66+
return ;
67+
}
68+
// Taken the character ;
69+
char ch=str.charAt(0);
70+
71+
// Each time the no of recursions we have to made are = size of temp + 1;
72+
// The character can be add at 3 pos eg
73+
// AB & /C rem.
74+
// _AB A_B AB_ = CAB ACB ABC
75+
for(int i=0;i<=temp.length();i++)
76+
{
77+
String first=temp.substring(0,i);
78+
String second=temp.substring(i);
79+
permutations(first+ch+second, str.substring(1));
80+
}
81+
}
82+
83+
static ArrayList<String> permutations2(String temp,String str,ArrayList<String>list)
84+
{
85+
if(str.length()==0)
86+
{
87+
list.add(temp);
88+
return list;
89+
}
90+
char ch=str.charAt(0);
91+
for(int i=0;i<=temp.length();i++)
92+
{
93+
String first=temp.substring(0,i);
94+
String second=temp.substring(i);
95+
permutations(first+ch+second, str.substring(1));
96+
}
97+
return list;
98+
}
99+
100+
public static void main(String[] args) {
101+
102+
System.out.println("---------Printing the subsequences of strings-----------");
103+
subSeq("","ABC");
104+
105+
System.out.println("---------Arraylist the subsequences of strings-----------");
106+
ArrayList<String> list1= subSeq2("","ABC", new ArrayList<String>());
107+
for (String string : list1) {
108+
System.out.println(string);
109+
}
110+
111+
System.out.println("---------Printing the Sub Arrays -----------");
112+
int arr[]= {1,2,3};
113+
System.out.println(SubArray(arr));
114+
115+
System.out.println("---------Printing the Permutations of strings-----------");
116+
permutations("","ABC");
117+
118+
System.out.println("---------Printing the Permutations of strings-----------");
119+
ArrayList<String> list2=permutations2("","ABC", new ArrayList<>());
120+
for (String string : list2) {
121+
System.out.println(string);
122+
}
123+
124+
125+
126+
}
127+
}

0 commit comments

Comments
 (0)