|
| 1 | +package Recursion; |
| 2 | + |
| 3 | +public class SkipLetter { |
| 4 | + |
| 5 | + private static String skip(String str, String target, int index) { |
| 6 | + String s = ""; |
| 7 | + if (index == str.length()) |
| 8 | + return s; |
| 9 | + if (target.equals(str.charAt(index) + "")) |
| 10 | + s = "" + skip(str, target, index + 1); |
| 11 | + else |
| 12 | + s = str.charAt(index) + skip(str, target, index + 1); |
| 13 | + return s; |
| 14 | + } |
| 15 | + |
| 16 | + private static String skip(String str, String target) { |
| 17 | + String s = ""; |
| 18 | + if (str.length() == 0) |
| 19 | + return s; |
| 20 | + if (target.equals(str.charAt(0) + "")) |
| 21 | + s = "" + skip(str.substring(1), target); |
| 22 | + else |
| 23 | + s = str.charAt(0) + skip(str.substring(1), target); |
| 24 | + return s; |
| 25 | + } |
| 26 | + |
| 27 | + private static void skip(String str, String target, String ans) { |
| 28 | + if (str.length() == 0) { |
| 29 | + System.out.println(ans); |
| 30 | + return; |
| 31 | + } |
| 32 | + if (target.equals(str.charAt(0) + "")) |
| 33 | + skip(str.substring(1), target, ans); |
| 34 | + else |
| 35 | + skip(str.substring(1), target, ans + str.charAt(0)); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + private static String skip(String str) { |
| 40 | + if (str.length() == 0) { |
| 41 | + return ""; |
| 42 | + } |
| 43 | + if (str.charAt(0) == 'a') |
| 44 | + return skip(str.substring(1)); |
| 45 | + else |
| 46 | + return str.charAt(0) + skip(str.substring(1)); |
| 47 | + } |
| 48 | + |
| 49 | + public static void main(String[] args) { |
| 50 | + System.out.println(skip("baccad", "a", 0)); |
| 51 | + System.out.println(skip("baccad", "a")); |
| 52 | + skip("baccadah", "a", ""); |
| 53 | + System.out.println(skip("baccadadeda")); |
| 54 | + } |
| 55 | +} |
0 commit comments