Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 86fe0b8

Browse files
add
1 parent 4d4a649 commit 86fe0b8

File tree

7 files changed

+266
-8
lines changed

7 files changed

+266
-8
lines changed

‎src/LeetcodeQuestions/SubsetSum.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package LeetcodeQuestions;
22

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

67
public class SubsetSum {
@@ -45,6 +46,51 @@ public static List<List<Integer>> combSum(int arr[], int tar) {
4546
}
4647

4748

49+
// Method 2------------>
50+
51+
public static void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums, int remain,
52+
int start) {
53+
if (remain < 0)
54+
return;
55+
else if (remain == 0)
56+
list.add(new ArrayList<>(tempList));
57+
else {
58+
for (int i = start; i < nums.length; i++) {
59+
tempList.add(nums[i]);
60+
backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can
61+
// reuse same elements
62+
tempList.remove(tempList.size() - 1);
63+
}
64+
}
65+
}
66+
67+
68+
// Method 2---> with duplicate values.. can't reuse same element
69+
public static void backtrack2(List<List<Integer>> list, List<Integer> tempList, int[] nums, int remain,
70+
int start) {
71+
if (remain < 0)
72+
return;
73+
else if (remain == 0)
74+
list.add(new ArrayList<>(tempList));
75+
else {
76+
for (int i = start; i < nums.length; i++) {
77+
if (i > start && nums[i] == nums[i - 1])
78+
continue; // skip duplicates
79+
tempList.add(nums[i]);
80+
backtrack2(list, tempList, nums, remain - nums[i], i + 1);
81+
tempList.remove(tempList.size() - 1);
82+
}
83+
}
84+
}
85+
86+
87+
public List<List<Integer>> helper2(int[] nums, int target) {
88+
List<List<Integer>> list = new ArrayList<>();
89+
Arrays.sort(nums);
90+
backtrack(list, new ArrayList<>(), nums, target, 0);
91+
return list;
92+
93+
}
4894

4995
public static void main(String[] args) {
5096

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package LeetcodeQuestions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
// https://leetcode.com/problems/palindrome-partitioning/
7+
public class palindromePartition {
8+
9+
10+
public static List<List<String>> partition(String s) {
11+
List<List<String>> list = new ArrayList<>();
12+
backtrack(list, new ArrayList<>(), s, 0);
13+
return list;
14+
}
15+
16+
public static void backtrack(List<List<String>> list, List<String> tempList, String s,
17+
int start) {
18+
if (start == s.length())
19+
list.add(new ArrayList<>(tempList));
20+
else {
21+
for (int i = start; i < s.length(); i++) {
22+
if (isPalindrome(s, start, i)) {
23+
tempList.add(s.substring(start, i + 1));
24+
backtrack(list, tempList, s, i + 1);
25+
tempList.remove(tempList.size() - 1);
26+
}
27+
}
28+
}
29+
}
30+
31+
public static boolean isPalindrome(String s, int low, int high) {
32+
while (low < high)
33+
if (s.charAt(low++) != s.charAt(high--))
34+
return false;
35+
return true;
36+
}
37+
38+
39+
public static void main(String[] args) {
40+
41+
for (List<String> li : partition("aab")) {
42+
43+
System.out.println(li);
44+
}
45+
}
46+
47+
}

‎src/LeetcodeQuestions/permutations.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package LeetcodeQuestions;
22

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

67
// https://leetcode.com/problems/permutations/
78

8-
9+
910
// https://github.com/shashankch/DataStructures-Algo-Java/blob/master/src/PrepStringPermuteBacktrack.java
1011

1112
public class permutations {
@@ -27,7 +28,7 @@ public static void permute(int[] nums, int i, List<List<Integer>> list) {
2728

2829
}
2930

30-
public static void swap(int[] nums, int i, int j) {
31+
public static void swap(int[] nums, int i, int j) {
3132
int temp = nums[i];
3233
nums[i] = nums[j];
3334
nums[j] = temp;
@@ -42,15 +43,58 @@ public static List<List<Integer>> helper(int[] nums) {
4243

4344

4445
}
45-
46+
47+
48+
// Method 2-------------->
49+
50+
public static void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums) {
51+
if (tempList.size() == nums.length) {
52+
list.add(new ArrayList<>(tempList));
53+
} else {
54+
for (int i = 0; i < nums.length; i++) {
55+
if (tempList.contains(nums[i]))
56+
continue; // element already exists, skip
57+
tempList.add(nums[i]);
58+
backtrack(list, tempList, nums);
59+
tempList.remove(tempList.size() - 1);
60+
}
61+
}
62+
}
63+
64+
// Method 2 with duplicate values...
65+
public static void backtrack2(List<List<Integer>> list, List<Integer> tempList, int[] nums,
66+
boolean[] used) {
67+
if (tempList.size() == nums.length) {
68+
list.add(new ArrayList<>(tempList));
69+
} else {
70+
for (int i = 0; i < nums.length; i++) {
71+
if (used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1])
72+
continue;
73+
used[i] = true;
74+
tempList.add(nums[i]);
75+
backtrack2(list, tempList, nums, used);
76+
used[i] = false;
77+
tempList.remove(tempList.size() - 1);
78+
}
79+
}
80+
}
81+
82+
83+
public List<List<Integer>> helper2(int[] nums) {
84+
List<List<Integer>> list = new ArrayList<>();
85+
Arrays.sort(nums); // sorting needed in duplicate values case
86+
backtrack2(list, new ArrayList<>(), nums, new boolean[nums.length]);
87+
return list;
88+
}
89+
4690

4791
public static void main(String[] args) {
48-
92+
4993
int arr[] = {1, 2, 3};
5094
for (List<Integer> li : helper(arr)) {
5195

5296
System.out.println(li);
53-
97+
5498
}
5599

56100
}

‎src/LeetcodeQuestions/subsets.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package LeetcodeQuestions;
22

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

67
public class subsets {
@@ -42,6 +43,7 @@ public static List<List<Integer>> helper(int arr[]) {
4243
return result;
4344
}
4445

46+
// Method 2----------->
4547
// O(N*2^N)
4648
public static List<List<Integer>> helper2(int arr[]) {
4749

@@ -64,6 +66,42 @@ public static List<List<Integer>> helper2(int arr[]) {
6466

6567
}
6668

69+
// Method 3--------->
70+
71+
public List<List<Integer>> helper3(int[] nums) {
72+
List<List<Integer>> list = new ArrayList<>();
73+
Arrays.sort(nums);
74+
backtrack(list, new ArrayList<>(), nums, 0);
75+
return list;
76+
}
77+
78+
public void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums,
79+
int start) {
80+
list.add(new ArrayList<>(tempList));
81+
for (int i = start; i < nums.length; i++) {
82+
tempList.add(nums[i]);
83+
backtrack(list, tempList, nums, i + 1);
84+
tempList.remove(tempList.size() - 1);
85+
}
86+
}
87+
88+
// for arr containing duplicate values..
89+
public void backtrack2(List<List<Integer>> list, List<Integer> tempList, int[] nums,
90+
int start) {
91+
list.add(new ArrayList<>(tempList));
92+
for (int i = start; i < nums.length; i++) {
93+
if (i > start && nums[i] == nums[i - 1])
94+
continue; // skip duplicates
95+
tempList.add(nums[i]);
96+
backtrack2(list, tempList, nums, i + 1);
97+
tempList.remove(tempList.size() - 1);
98+
}
99+
}
100+
101+
102+
103+
104+
67105

68106
public static void main(String[] args) {
69107

‎src/arrays/PairSum.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Arrays;
44

55
public class PairSum {
6-
public static void pairSum(int[] arr, int num) {
6+
public static void pairSum1(int[] arr, int num) {
77

88
Arrays.sort(arr);
99

@@ -33,4 +33,36 @@ public static void pairSum(int[] arr, int num) {
3333
}
3434

3535
}
36+
37+
38+
public static void pairSum2(int[] input, int x) {
39+
40+
for (int i = 0; i < input.length - 1; i++) {
41+
42+
for (int j = i + 1; j < input.length; j++)
43+
44+
{
45+
46+
if (input[i] + input[j] == x) {
47+
48+
if (input[i] < input[j]) {
49+
System.out.println(input[i] + " " + input[j]);
50+
} else {
51+
System.out.println(input[j] + " " + input[i]);
52+
}
53+
54+
}
55+
56+
57+
58+
}
59+
}
60+
61+
62+
63+
}
64+
65+
public static void main(String[] args) {
66+
67+
}
3668
}

‎src/arrays/sort01.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11

22
package arrays;
33

4-
public class sort01{
4+
public class sort01{
55

6+
7+
public static void helper2(int[] arr) {
8+
9+
10+
int l = 0;
11+
int h = arr.length - 1;
12+
13+
while (l < h) {
14+
15+
16+
if (arr[l] == 0) {
17+
l++;
18+
} else if (arr[h] == 1) {
19+
h--;
20+
} else if (arr[l] == 1 && arr[h] == 0) {
21+
22+
int temp = arr[l];
23+
arr[l] = arr[h];
24+
arr[h] = temp;
25+
}
26+
27+
28+
29+
}
30+
31+
}
632

733
public static void main(String[] args) {
834

‎src/arrays/tripletsum.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ public static void tripletfun(int arr[], int n, int x) {
6565

6666
}
6767

68+
public static void tripletfun2(int[] input, int x) {
69+
70+
Arrays.sort(input);
71+
for (int i = 0; i < input.length; i++) {
72+
73+
for (int j = i + 1; j < input.length; j++) {
74+
75+
for (int k = j + 1; k < input.length; k++) {
76+
77+
if (input[i] + input[j] + input[k] == x) {
78+
79+
System.out.println(input[i] + " " + input[j] + " " + input[k]);
80+
81+
}
82+
83+
84+
}
85+
}
86+
87+
88+
}
89+
90+
91+
}
92+
6893
public static void main(String[] args) {
6994

7095
Scanner sc = new Scanner(System.in);
@@ -83,4 +108,4 @@ public static void main(String[] args) {
83108

84109
}
85110

86-
}
111+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /