-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecursion-9
More file actions
199 lines (176 loc) · 6.21 KB
/
Recursion-9
File metadata and controls
199 lines (176 loc) · 6.21 KB
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
Q1 - You are given a string. Your task is to divide the string into palindromic substrings. Print all such
partitions.
Example
= banana
= [b, a, n, a, n, a]
[b, a, n, ana]
[b, a, nan, a]
[b, ana, n, a]
[b, anana]
import java.util.*;
public class Main {
public static boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left <= right) {
if (s.charAt(left) != s.charAt(right))
return false;
left++;
right--;
}
return true;
}
public static void printAllPalindromicPartitions(String s, ArrayList<String> ans, int start) {
if (start == s.length()) {
System.out.println(ans);
return;
}
String curr = "";
for (int end = start; end < s.length(); end++) {
curr += s.charAt(end);
if (isPalindrome(curr)) {
ans.add(curr);
printAllPalindromicPartitions(s, ans, end + 1);
ans.remove(ans.size() - 1);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
ArrayList<String> ans = new ArrayList<>();
printAllPalindromicPartitions(s, ans, 0);
}
}
Q2 - A string is called beautiful if is an even length string consisting of only stars(‘*’) and dashes(‘-’). Further
the number of stars in the first half of the string should be equal to the number of stars in the second half of
the string. Given a number n, print all the beautiful strings of length 2 * n.
import java.util.*;
public class Main {
public static void printCombinations(ArrayList<String> nums) {
for (String num1 : nums) {
for (String num2 : nums) {
System.out.println(num1 + num2);
}
}
}
public static void generateStrings(int remainingChars, ArrayList<ArrayList<String>> nums, int numberOfStars, String curr) {
if (remainingChars == 0) {
nums.get(numberOfStars).add(curr);
return;
}
generateStrings(remainingChars - 1, nums, numberOfStars + 1, curr + "*");
generateStrings(remainingChars - 1, nums, numberOfStars, curr + "-");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<ArrayList<String>> nums = new ArrayList<ArrayList<String>>();
for (int i = 0; i <= n; i++) {
nums.add(new ArrayList<String>());
}
generateStrings(n, nums, 0, "");
for (int s = 0; s <= n; s++) {
printCombinations(nums.get(s));
}
}
}
Q3 - A string is called beautiful if it consists of only stars(‘*’) and dashes(‘-’). Further the number of stars in
the first half of the string should be equal to the number of stars in the second half of the string. Given a
number n, print all the beautiful strings of length n. If the value of n is odd, the middle value can be either
'*' or '-'
import java.util.*;
public class Main {
public static void printCombinationsOdd(ArrayList<String> nums) {
for (String num1 : nums) {
for (String num2 : nums) {
System.out.println(num1 + "*" + num2);
System.out.println(num1 + "-" + num2);
}
}
}
public static void printCombinationsEven(ArrayList<String> nums) {
for (String num1 : nums) {
for (String num2 : nums) {
System.out.println(num1 + num2);
}
}
}
public static void generateStrings(int remainingBits, ArrayList<ArrayList<String>> nums, int numberOfStars, String curr) {
if (remainingBits == 0) {
nums.get(numberOfStars).add(curr);
return;
}
generateStrings(remainingBits - 1, nums, numberOfStars + 1, curr + "*");
generateStrings(remainingBits - 1, nums, numberOfStars, curr + "-");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<ArrayList<String>> nums = new ArrayList<ArrayList<String>>();
for (int i = 0; i <= n; i++) {
nums.add(new ArrayList<String>());
}
generateStrings(n / 2, nums, 0, "");
if (n % 2 == 1)
for (int s = 0; s <= n; s++)
printCombinationsOdd(nums.get(s));
else
for (int s = 0; s <= n; s++)
printCombinationsEven(nums.get(s));
}
}
Q4 - Problem Count the number of substrings having same first and last characters
Input :
Output :
s = "pqrpq"
7
import java.util.*;
public class Main {
public static int count(String s, int i, int j, int n) {
// base cases
if (n == 1)
return 1;
if (n <= 0)
return 0;
int ans = count(s, i + 1, j, n - 1) +
count(s, i, j - 1, n - 1) -
count(s, i + 1, j - 1, n - 2);
if (s.charAt(i) == s.charAt(j))
ans++;
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
int n = s.length();
System.out.println(count(s, 0, n - 1, n));
}
}
Q5 - You are given a string s. All the characters in s are distinct. Your task is to generate all the strings that
contain the characters of ‘s’ in increasing order.
import java.util.*;
public class Main {
public static void generateSubstrings(String str, int n, int index, String curr) {
if (index == n) {
return;
}
System.out.println(curr);
for (int i = index + 1; i < n; i++) {
curr += str.charAt(i);
generateSubstrings(str, n, i, curr);
curr = curr.substring(0, curr.length() - 1);
}
return;
}
public static String sort(String str) {
char[] charArr = str.toCharArray();
Arrays.sort(charArr);
return new String(charArr);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
s = sort(s);
generateSubstrings(s, s.length(), -1, "");
}
}