-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeavel6_Subset_Subsequence_String.java
200 lines (166 loc) · 6.22 KB
/
Leavel6_Subset_Subsequence_String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Subsets :- permutations and combination
// subset means non adjacent collection
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Leavel6_Subset_Subsequence_String {
static void subSequence(String str,int index,String newString){
if (index == str.length()){
System.out.println(newString);
return;
}
char currChar = str.charAt(index);
// to be add
subSequence(str,index+1,newString+currChar);
// not to be add
subSequence(str,index+1,newString);
}
static void printPermutation(String str,String permutation){
if (str.length() == 0){
System.out.println(permutation);
return;
}
for (int i=0;i<str.length();i++){
char curr = str.charAt(i);
// below we do + str.substring(i+1) beacuse substring does not contain last index hence we do manually to increase it by 1...
String newStr = str.substring(0,i)+str.substring(i+1);
printPermutation(newStr,permutation+curr);
}
}
public static void main(String[] args) {
printPermutation("abc","");
// subSequence("abc",0,"");
// skip("","baccdah");
// System.out.println(skip("bccdfhfgha"));
// System.out.println(skipApple("bcdlapple"));
// subSet("","abc");
// subSetWithAscii("","abc");
// System.out.println(subSetReturn("","abc"));
// System.out.println(subSetAsciiReturn("","abc"));
// int []arr2 = {1,2,3};
// List<List<Integer>> ans2 = subsetArray(arr2);
// for(List<Integer> list : ans2){
// System.out.println(list);
// }
// int []arr = {1,2,2};
// List<List<Integer>> ans = subsetArrayDuplicate(arr);
// for(List<Integer> list : ans){
// System.out.println(list);
// }
}
static void skip(String initialString,String givenString){
// base case..
if(givenString.isEmpty()){
System.out.println(initialString);
return;
}
char firstChar = givenString.charAt(0);
if (firstChar == 'a'){
skip(initialString,givenString.substring(1));
}else {
skip(initialString+firstChar,givenString.substring(1));
}
}
static String skip(String givenString){
if(givenString.isEmpty()){
return "";
}
char firstChar = givenString.charAt(0);
if (firstChar == 'a'){
return skip(givenString.substring(1));
}else {
return firstChar + skip(givenString.substring(1));
}
}
static String skipApple(String givenString){
if(givenString.isEmpty()){
return "";
}
if (givenString.startsWith("apple")){
return skipApple(givenString.substring(5));
}else {
return givenString.charAt(0) + skipApple(givenString.substring(1));
}
}
// Subset question using recursion as well as iteration...
static void subSet(String initialString,String givenString){
if (givenString.isEmpty()){
System.out.println(initialString);
return;
}
char firstChar = givenString.charAt(0);
subSet(initialString+firstChar,givenString.substring(1)); // call left side first
subSet(initialString,givenString.substring(1));
}
static ArrayList<String> subSetReturn(String initialString, String givenString){
if (givenString.isEmpty()){
ArrayList<String>list = new ArrayList<>();
list.add(initialString);
return list;
}
char firstChar = givenString.charAt(0);
ArrayList<String> left = subSetReturn(initialString+firstChar,givenString.substring(1)); // call left side first
ArrayList<String> right = subSetReturn(initialString,givenString.substring(1));
left.addAll(right);
return left;
}
static void subSetWithAscii(String initialString,String givenString){
if (givenString.isEmpty()){
System.out.println(initialString);
return;
}
char firstChar = givenString.charAt(0);
subSetWithAscii(initialString+firstChar,givenString.substring(1)); // call left side first
subSetWithAscii(initialString,givenString.substring(1));
subSetWithAscii(initialString+(firstChar+0),givenString.substring(1));
}
static ArrayList<String> subSetAsciiReturn(String initialString, String givenString){
if (givenString.isEmpty()){
ArrayList<String>list = new ArrayList<>();
list.add(initialString);
return list;
}
char firstChar = givenString.charAt(0);
ArrayList<String> first = subSetReturn(initialString+firstChar,givenString.substring(1)); // call left side first
ArrayList<String> second = subSetReturn(initialString,givenString.substring(1));
ArrayList<String> third = subSetReturn(initialString+(firstChar+0),givenString.substring(1));
first.addAll(second);
first.addAll(third);
return first;
}
// Using iteration using array....
static List<List<Integer>> subsetArray(int []arr) {
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
for (int num : arr){
int n = outer.size();
for (int i=0;i<n;i++){
List<Integer> internal = new ArrayList<>(outer.get(i));
internal.add(num);
outer.add(internal);
}
}
return outer;
}
static List<List<Integer>> subsetArrayDuplicate(int []arr) {
Arrays.sort(arr);
List<List<Integer>> outer = new ArrayList<>();
outer.add(new ArrayList<>());
int start = 0;
int end = 0;
for (int i = 0;i<arr.length;i++){
start = 0;
if (i>0 && arr[i] == arr[i-1]){
start = end+1;
}
end = outer.size()-1;
int n = outer.size();
for (int j=start;j<n;j++){
List<Integer> internal = new ArrayList<>(outer.get(j));
internal.add(arr[i]);
outer.add(internal);
}
}
return outer;
}
}