Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions telegramRecursionQuestions/ReverseAString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package assignments.telegramRecursionQuestions;

public class ReverseAString {

public static void main(String[] args) {
String s = "ApniKaksha";
reverse(s,0,s.length()-1);

}

public static void reverse(String s, int l , int r) {

/* Base Condition is basically when the length of the string is
* 1 - where we simply print the string.
* 2 - where we exchange the characters and then print the string.
*/
if(l>=r) {
System.out.println(s);
return;
}

// In the following code we are exchanging the characters of the string pairwise from the end.

char arr[] = s.toCharArray();
char temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
s = String.valueOf(arr);
reverse(s,l+1,r-1);



}

}
40 changes: 40 additions & 0 deletions telegramRecursionQuestions/StringPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package assignments.telegramRecursionQuestions;

public class StringPalindrome {

public static void main(String[] args) {
String s = "Naman";
if(isPalindrome(s,0,s.length()-1)) {
System.out.println("Yes");
}else {
System.out.println("No");
}

}

public static boolean isPalindrome(String g, int l, int r) {

/* Base condition is when the length of the string is
* 1 - which implies that the string will be a palindrome.
* 0 - which implies that there is no string so it can not be a palindrome.
*/


String s = g.toLowerCase();
char[] arr = s.toCharArray();
if (arr.length==0) return false;
if (arr.length==1) return true;


if(arr[l]==arr[r] && l<=r) {
isPalindrome(s,l+1,r-1);
return true;


}


return false;
}

}
42 changes: 42 additions & 0 deletions telegramRecursionQuestions/SubSequencesOfString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package assignments.telegramRecursionQuestions;
import java.util.*;
public class SubSequencesOfString {
//Print all the subsequences of a string using recursion.
static Set<String> sett = new HashSet<>();
public static void main(String[] args) {



String s = "hello";


for(String a: getSubSequences(s,0)) {
System.out.println(a);
}


}

public static HashSet<String> getSubSequences(String s, int i){

if(i>=s.length()) {
return (HashSet<String>) sett;
}

int j = i+1;
while(j<=s.length()) {

sett.add(s.substring(i,j));

j++;

}
getSubSequences(s, i+1);
return (HashSet<String>) sett;

}}