Skip to content

Commit 000e6da

Browse files
LeetCode Problems on SubSequence
1 parent 5924a32 commit 000e6da

File tree

1 file changed

+148
-71
lines changed

1 file changed

+148
-71
lines changed

src/strings/SubSet.java

Lines changed: 148 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package strings;
22

33
import java.util.ArrayList;
4+
import java.util.Arrays;
45
import java.util.List;
56

67
public class SubSet {
7-
8-
//Normal fakt string cut karat jato
8+
9+
// Normal fakt string cut karat jato
910
static void subSeq(String temp, String str) {
1011
if (str.length() == 0) {
1112
System.out.println(temp);
@@ -14,114 +15,190 @@ static void subSeq(String temp, String str) {
1415
subSeq(temp + str.charAt(0), str.substring(1));
1516
subSeq(temp, str.substring(1));
1617
}
17-
18+
1819
// ARraylist return
19-
static ArrayList<String> subSeq2(String temp, String str,ArrayList< String> list) {
20+
static ArrayList<String> subSeq2(String temp, String str, ArrayList<String> list) {
2021
if (str.length() == 0) {
2122
list.add(temp);
22-
return list ;
23+
return list;
2324
}
24-
subSeq(temp + str.charAt(0), str.substring(1));
25-
subSeq(temp, str.substring(1));
25+
subSeq2(temp + str.charAt(0), str.substring(1), list);
26+
subSeq2(temp, str.substring(1), list);
2627
return list;
2728
}
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<>();
29+
30+
static List<List<Integer>> SubArray(int arr[]) {
31+
List<List<Integer>> outer = new ArrayList<List<Integer>>();
32+
ArrayList<Integer> list = new ArrayList<>();
3433
outer.add(list);
35-
36-
// Each element of array should be added in the each element present inside the
34+
35+
// Each element of array should be added in the each element present inside the
3736
// 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-
37+
38+
for (int num : arr) {
39+
// Taken Size here because we are adding elments in the loop
40+
int size = outer.size();
41+
4442
// 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);
43+
44+
for (int i = 0; i < size; i++) {
45+
ArrayList<Integer> internal = new ArrayList<>();
46+
internal.addAll(outer.get(i));
47+
internal.add(num);
48+
outer.add(internal);
5249
}
5350
}
5451

55-
5652
return outer;
57-
53+
5854
}
59-
60-
61-
static void permutations(String temp,String str)
62-
{
63-
if(str.length()==0)
64-
{
55+
56+
static void permutations(String temp, String str) {
57+
if (str.length() == 0) {
6558
System.out.println(temp);
66-
return ;
59+
return;
6760
}
6861
// Taken the character ;
69-
char ch=str.charAt(0);
70-
62+
char ch = str.charAt(0);
63+
7164
// Each time the no of recursions we have to made are = size of temp + 1;
7265
// The character can be add at 3 pos eg
7366
// AB & /C rem.
7467
// _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));
68+
for (int i = 0; i <= temp.length(); i++) {
69+
String first = temp.substring(0, i);
70+
String second = temp.substring(i);
71+
permutations(first + ch + second, str.substring(1));
8072
}
8173
}
82-
83-
static ArrayList<String> permutations2(String temp,String str,ArrayList<String>list)
84-
{
85-
if(str.length()==0)
86-
{
87-
list.add(temp);
74+
75+
static ArrayList<String> permutations2(String temp, String str, ArrayList<String> list) {
76+
if (str.length() == 0) {
77+
list.add(temp);
8878
return list;
8979
}
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));
80+
char ch = str.charAt(0);
81+
for (int i = 0; i <= temp.length(); i++) {
82+
String first = temp.substring(0, i);
83+
String second = temp.substring(i);
84+
permutations(first + ch + second, str.substring(1));
9685
}
9786
return list;
9887
}
99-
100-
public static void main(String[] args) {
88+
89+
// LeetCode
90+
// https://leetcode.com/problems/permutation-in-string/
91+
// Given two strings s1 and s2, return true if s2 contains a permutation of s1,
92+
// or false otherwise.
93+
// In other words, return true if one of s1's permutations is the substring of
94+
// s2.
95+
96+
public boolean checkInclusion(String s1, String s2) {
97+
List<String> op = chkPermPresent("", s1, new ArrayList());
98+
for (int i = 0, j = op.size() - 1; i <= j; i++, j--) {
99+
if (s2.contains(op.get(i)) || s2.contains(op.get(j)))
100+
return true;
101+
}
102+
return false;
103+
}
104+
105+
List<String> chkPermPresent(String temp, String str, ArrayList<String> list) {
106+
if (str.length() == 0) {
107+
list.add(temp);
108+
return list;
109+
}
110+
111+
char ch = str.charAt(0);
112+
for (int i = 0; i <= temp.length(); i++) {
113+
String first = temp.substring(0, i);
114+
String secons = temp.substring(i);
115+
chkPermPresent(first + ch + secons, str.substring(1), list);
116+
}
117+
return list;
118+
119+
}
120+
121+
// LeetCode
122+
// https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
123+
// Given two strings s and p, return an array of all the start indices of p's
124+
// anagrams in s.
125+
// You may return the answer in any order.
126+
public List<Integer> findAnagrams(String s, String p) {
127+
List<Integer> list = new ArrayList<>();
128+
char arr1[] = p.toCharArray();
129+
Arrays.sort(arr1);
130+
for (int i = 0; i < s.length() - (p.length() - 1); i++) {
131+
char arr2[] = s.substring(i, i + p.length()).toCharArray();
132+
Arrays.sort(arr2);
133+
if (Arrays.toString(arr1).equals(Arrays.toString(arr2))) {
134+
list.add(i);
135+
}
136+
137+
}
138+
return list;
139+
}
140+
141+
// LeetCode https://leetcode.com/problems/longest-palindromic-subsequence/
142+
// Given a string s, find the longest palindromic subsequence's length in s.
143+
public int longestPalindromeSubseq(String s) {
144+
int max = -1;
145+
146+
for (String str : (List<String>) subSeq2("", s, new ArrayList())) {
147+
if (chkPali(str) && str.length() > max) {
148+
max = str.length();
149+
}
150+
}
151+
return max;
152+
}
153+
154+
boolean chkPali(String str) {
155+
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
156+
if (str.charAt(i) != str.charAt(j))
157+
return false;
158+
}
159+
return true;
160+
}
161+
162+
// LeetCode https://leetcode.com/problems/longest-common-subsequence/description/
163+
// Longest Common SubSequence from two Strings
164+
public int longestCommonSubsequence(String text1, String text2) {
165+
166+
int max = 0;
167+
List<String> first = subSeq2("", text1, new ArrayList());
168+
List<String> second = subSeq2("", text2, new ArrayList());
101169

170+
for(String s:first)
171+
{
172+
if(second.toString().contains(s)&&s.length()>max)
173+
max=s.length();
174+
}
175+
return max;
176+
177+
}
178+
179+
public static void main(String[] args) {
180+
102181
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>());
182+
subSeq("", "ABC");
183+
184+
System.out.println("---------Arraylist the subsequences of strings-----------");
185+
ArrayList<String> list1 = subSeq2("", "ABC", new ArrayList<String>());
107186
for (String string : list1) {
108187
System.out.println(string);
109188
}
110-
189+
111190
System.out.println("---------Printing the Sub Arrays -----------");
112-
int arr[]= {1,2,3};
191+
int arr[] = { 1, 2, 3 };
113192
System.out.println(SubArray(arr));
114-
193+
115194
System.out.println("---------Printing the Permutations of strings-----------");
116-
permutations("","ABC");
117-
195+
permutations("", "ABC");
196+
118197
System.out.println("---------Printing the Permutations of strings-----------");
119-
ArrayList<String> list2=permutations2("","ABC", new ArrayList<>());
198+
ArrayList<String> list2 = permutations2("", "ABC", new ArrayList<>());
120199
for (String string : list2) {
121200
System.out.println(string);
122201
}
123-
124-
125-
202+
126203
}
127204
}

0 commit comments

Comments
 (0)