└── Drill08.java /Drill08.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | /* 5 | * For this drill, you will get practice with recursive backtracking. As with 6 | * the previous drill that emphasized recursion, this drill will be more difficult 7 | * than other drills for many students. Start early! 8 | * 9 | * Remember these are all recursive backtracking problems. So make sure to ask yourself 10 | * the questions detailed in the slides. These questions will lead you to the answer. 11 | * All of the code should be in the general pseudocode shown in the slides. A shortened 12 | * version: 13 | * ------------------------------- 14 | * Base Case(s) - if all decisions have been made 15 | * 16 | * Recursive Case - must go through all decisions possible at that point in time 17 | * Choose - choose one decision out of all the options 18 | * Explore - recurse, trying to solve the problem with the choice you just made 19 | * Unchoose - need to undo the choice you made above so that you can try another choice! 20 | */ 21 | public class Drill08 { 22 | 23 | 24 | /* Write a recursive function named canMakeSum that takes a list of 25 | * integers and an integer target value (sum) and returns true if it is 26 | * possible to have some set of values from the list that sum to the 27 | * target value. 28 | * 29 | * For example, the list {2,1,1,3,5} and target value 9 should return true 30 | * (5 + 3 + 1 = 9). 31 | * However, the list {5,4,1,6} and target value 8 should return false. 32 | */ 33 | public static boolean canMakeSum(ArrayList list, int sum) { 34 | if (sum == 0) { 35 | return true; 36 | } 37 | else if (list.size() == 0) { 38 | return false; 39 | } 40 | 41 | if (list.get(list.size() - 1) > sum) { 42 | list.remove(list.size() - 1); 43 | return canMakeSum(list, sum); 44 | } 45 | else { 46 | return canMakeSum(list, sum - list.remove(list.size() - 1)); 47 | } 48 | } 49 | 50 | 51 | 52 | /* Write a recursive function named longestCommonSubsequence that returns the 53 | * longest common subsequence of two strings. Recall that if a string is a subsequence 54 | * of another, each of its letters occurs in the longer string in the same order, but 55 | * not necessarily consecutively. 56 | * 57 | * Some examples: 58 | * longestCommonSubsequence("tyler", "kate") -> "te" 59 | * longestCommonSubsequence("hannah", "banana") "anna" 60 | * longestCommonSubsequence("she sells", "seashells") "sesells" 61 | * longestCommonSubsequence("CS210", "arizona") "" 62 | */ 63 | public static String longestCommonSubsequence(String s1, String s2) { 64 | if (s1.length() == 0 || s2.length() == 0) { 65 | return ""; 66 | } 67 | if (s1.charAt(s1.length() - 1) == (s2.charAt(s2.length() - 1))) { 68 | String substring1 = s1.substring(0, s1.length() - 1); 69 | String substring2 = s2.substring(0, s2.length() - 1); 70 | return longestCommonSubsequence(substring1, 71 | substring2) + s2.charAt(s2.length() - 1); 72 | } 73 | else { 74 | String substring1 = s1.substring(0, s1.length() - 1); 75 | String substring2 = s2.substring(0, s2.length() - 1); 76 | String result1 = longestCommonSubsequence(s1, substring2); 77 | String result2 = longestCommonSubsequence(substring1, s2); 78 | if (result1.length() > result2.length()) { 79 | return result1; 80 | } 81 | else { 82 | return result2; 83 | } 84 | } 85 | } 86 | 87 | 88 | 89 | 90 | /* Write a recursive function named editDistance that accepts two string 91 | * parameters and returns the "edit distance" between the two strings as an 92 | * integer. Edit distance (also called Levenshtein distance) is the minimum 93 | * number of "changes" required to get from s1 to s2 or vice versa. A "change" 94 | * is a) inserting a character, 95 | * b) deleting a character, or 96 | * c) changing a character to a different character. 97 | * 98 | * Some examples: 99 | * editDistance("driving", "diving") -> 1 100 | * editDistance("debate", "irate") -> 3 101 | * editDistance("football", "cookies") -> 6 102 | */ 103 | public static int editDistance(String s1, String s2) { 104 | if (s1.equals(s2)) { 105 | return 0; 106 | } 107 | else if (s1.length() == 0 && s2.length() != 0) { 108 | return s2.length(); 109 | } 110 | else if (s2.length() == 0 && s1.length() != 0) { 111 | return s1.length(); 112 | } 113 | if ((s1.charAt(s1.length() - 1) == s2.charAt(s2.length() - 1)) || 114 | (s1.length() >= 2 && s1.charAt(s1.length() - 2) == s2.charAt(s2.length() - 1)) || 115 | (s2.length() >= 2 && s1.charAt(s1.length() - 1) == s2.charAt(s2.length() - 2))) { 116 | String substring1 = s1.substring(0, s1.length() - 1); 117 | String substring2 = s2.substring(0, s2.length() - 1); 118 | return editDistance(substring1, substring2); 119 | } 120 | else { 121 | return 1 + editDistance(s1.substring(0, s1.length() - 1), 122 | s2.substring(0, s2.length() - 1)); 123 | } 124 | } 125 | 126 | } 127 | --------------------------------------------------------------------------------