diff --git a/telegramRecursionQuestions/ReverseAString.java b/telegramRecursionQuestions/ReverseAString.java new file mode 100644 index 0000000..4ee1a68 --- /dev/null +++ b/telegramRecursionQuestions/ReverseAString.java @@ -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); + + + + } + +} diff --git a/telegramRecursionQuestions/StringPalindrome.java b/telegramRecursionQuestions/StringPalindrome.java new file mode 100644 index 0000000..290ac73 --- /dev/null +++ b/telegramRecursionQuestions/StringPalindrome.java @@ -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; + } + +} diff --git a/telegramRecursionQuestions/SubSequencesOfString.java b/telegramRecursionQuestions/SubSequencesOfString.java new file mode 100644 index 0000000..d83b946 --- /dev/null +++ b/telegramRecursionQuestions/SubSequencesOfString.java @@ -0,0 +1,42 @@ +package assignments.telegramRecursionQuestions; +import java.util.*; +public class SubSequencesOfString { +//Print all the subsequences of a string using recursion. + static Set 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 getSubSequences(String s, int i){ + + if(i>=s.length()) { + return (HashSet) sett; + } + + int j = i+1; + while(j<=s.length()) { + + sett.add(s.substring(i,j)); + + j++; + + } + getSubSequences(s, i+1); + return (HashSet) sett; + + }} + + + + +