Skip to content

Commit a2ad005

Browse files
subset part 1
1 parent ab0500c commit a2ad005

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
1.65 MB
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.kunal.strings;
2+
3+
public class Ascii {
4+
public static void main(String[] args) {
5+
char ch = 'a';
6+
System.out.println((char)(ch + 1));
7+
}
8+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.kunal.strings;
2+
3+
public class Stream {
4+
public static void main(String[] args) {
5+
System.out.println(skipAppNotApple("bacapplcdah"));
6+
}
7+
8+
static void skip(String p, String up) {
9+
if (up.isEmpty()) {
10+
System.out.println(p);
11+
return;
12+
}
13+
char ch = up.charAt(0);
14+
15+
if (ch == 'a') {
16+
skip(p, up.substring(1));
17+
} else {
18+
skip(p + ch, up.substring(1));
19+
}
20+
}
21+
22+
static String skip(String up) {
23+
if (up.isEmpty()) {
24+
return "";
25+
}
26+
27+
char ch = up.charAt(0);
28+
29+
if (ch == 'a') {
30+
return skip(up.substring(1));
31+
} else {
32+
return ch + skip(up.substring(1));
33+
}
34+
}
35+
36+
static String skipApple(String up) {
37+
if (up.isEmpty()) {
38+
return "";
39+
}
40+
if (up.startsWith("apple")) {
41+
return skipApple(up.substring(5));
42+
} else {
43+
return up.charAt(0) + skipApple(up.substring(1));
44+
}
45+
}
46+
47+
static String skipAppNotApple(String up) {
48+
if (up.isEmpty()) {
49+
return "";
50+
}
51+
if (up.startsWith("app") && !up.startsWith("apple")) {
52+
return skipAppNotApple(up.substring(3));
53+
} else {
54+
return up.charAt(0) + skipAppNotApple(up.substring(1));
55+
}
56+
}
57+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.kunal.strings;
2+
3+
import java.util.ArrayList;
4+
5+
public class SubSeq {
6+
public static void main(String[] args) {
7+
// subseqAscii("", "abc");
8+
System.out.println(subseqAsciiRet("", "abc"));
9+
}
10+
11+
static void subseq(String p, String up) {
12+
if (up.isEmpty()) {
13+
System.out.println(p);
14+
return;
15+
}
16+
char ch = up.charAt(0);
17+
subseq(p + ch, up.substring(1));
18+
subseq(p, up.substring(1));
19+
}
20+
21+
static ArrayList<String> subseqRet(String p, String up) {
22+
if (up.isEmpty()) {
23+
ArrayList<String> list = new ArrayList<>();
24+
list.add(p);
25+
return list;
26+
}
27+
char ch = up.charAt(0);
28+
ArrayList<String> left = subseqRet(p + ch, up.substring(1));
29+
ArrayList<String> right = subseqRet(p, up.substring(1));
30+
31+
left.addAll(right);
32+
return left;
33+
}
34+
35+
static void subseqAscii(String p, String up) {
36+
if (up.isEmpty()) {
37+
System.out.println(p);
38+
return;
39+
}
40+
char ch = up.charAt(0);
41+
subseqAscii(p + ch, up.substring(1));
42+
subseqAscii(p, up.substring(1));
43+
subseqAscii(p + (ch+0), up.substring(1));
44+
}
45+
46+
static ArrayList<String> subseqAsciiRet(String p, String up) {
47+
if (up.isEmpty()) {
48+
ArrayList<String> list = new ArrayList<>();
49+
list.add(p);
50+
return list;
51+
}
52+
char ch = up.charAt(0);
53+
ArrayList<String> first = subseqAsciiRet(p + ch, up.substring(1));
54+
ArrayList<String> second = subseqAsciiRet(p, up.substring(1));
55+
ArrayList<String> third = subseqAsciiRet(p + (ch+0), up.substring(1));
56+
57+
first.addAll(second);
58+
first.addAll(third);
59+
return first;
60+
}
61+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.kunal.strings;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public class SubSet {
8+
public static void main(String[] args) {
9+
int[] arr = {1, 2, 2};
10+
List<List<Integer>> ans = subsetDuplicate(arr);
11+
for (List<Integer> list : ans) {
12+
System.out.println(list);
13+
}
14+
}
15+
16+
static List<List<Integer>> subset(int[] arr) {
17+
List<List<Integer>> outer = new ArrayList<>();
18+
outer.add(new ArrayList<>());
19+
for (int num : arr) {
20+
int n = outer.size();
21+
for (int i = 0; i < n; i++) {
22+
List<Integer> internal = new ArrayList<>(outer.get(i));
23+
internal.add(num);
24+
outer.add(internal);
25+
}
26+
}
27+
return outer;
28+
}
29+
30+
static List<List<Integer>> subsetDuplicate(int[] arr) {
31+
Arrays.sort(arr);
32+
List<List<Integer>> outer = new ArrayList<>();
33+
outer.add(new ArrayList<>());
34+
int start = 0;
35+
int end = 0;
36+
for (int i = 0; i < arr.length; i++) {
37+
start = 0;
38+
// if current and previous element is same, s = e + 1
39+
if (i > 0 && arr[i] == arr[i-1]) {
40+
start = end + 1;
41+
}
42+
end = outer.size() - 1;
43+
int n = outer.size();
44+
for (int j = start; j < n; j++) {
45+
List<Integer> internal = new ArrayList<>(outer.get(j));
46+
internal.add(arr[i]);
47+
outer.add(internal);
48+
}
49+
}
50+
return outer;
51+
}
52+
}

0 commit comments

Comments
 (0)