Hello, I am working on an exercise that's giving me a bit of a problem. The goal is to output the number of letters two Strings have in common. I've thought of a method where I take the Strings, throw them into a method that sorts them, then have another method to the removing of any extra letters. Example: (aabccde) -> (abcde).
For the most part, what I have thus far does work for "most" scenarios, but not all. The String, "aaabcdd" works fine within the method and is returned as "abcd" yet, "aabc" isn't returned as "abc". I've tried changined the length within the loops, the number of times it itterates, avoiding going out of bounds. I'm not sure what needs to be changed. Also, there is more than likely a more efficient way of doing what I'm trying to do, perhaps using what's called "Java regex" but I'm not at the level of having learned that yet. Here's what I have thus far below:
Note: Ignore the comments, they're more for me so that I don't forget what does what or what I'm trying to accomplish.
// Currently working on writing code that will output the number of letters, two Strings share in common import java.util.Arrays; public class TestingCode { public static void main(String[] args) { String str1 = "aabc"; String str2 = "aaabcdd"; System.out.println(lettersCommon(str1, str2)); }//end main public static int lettersCommon(String str1, String str2) { int count = 0; // We first sort the given Strings using another method called 'sortString' str1 = sortString(str1); str2 = sortString(str2); // We then remove the repeats within the sorted string using a method called 'removeRepeatLetters' str1 = removeRepeatLetters(str1); str2 = removeRepeatLetters(str2); // For simplicity, we store the lengths of both Strings here int len1 = str1.length(); int len2 = str2.length(); // We count the number of letters in common between both Strings below and return the number of letters in common if(len1 >= len2) { for(int i = 0; i < len2; i++) { for(int j = 0; j < len1; j++) { if(str2.charAt(i) == str1.charAt(j)) count++; } } }else { for(int i = 0; i < len1; i++) { for(int j = 0; j < len2; j++) { if(str1.charAt(i) == str2.charAt(j)) count++; } } } return count; } // Removes repeated letters in String (needs fixing) public static String removeRepeatLetters(String str) { // 'result' is used to store the String w/o repeated letters String strNoRepeat = ""; // Length of String given to method int len = str.length(); // Removing repeated letters from str1 for(int i=0; i < len-1; i++) { if(!(str.charAt(i) == str.charAt(i+1))) { strNoRepeat += str.substring(i,i+2); i++; } } System.out.println(strNoRepeat); return strNoRepeat; } // Sorts Strings (works) public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return String.valueOf(tempArray); } }//end class
Fixed Version:
// Currently working on writing code that will output the number of letters, two Strings share in common import java.util.Arrays; public class TestingCode { public static void main(String[] args) { String str1 = "ababc"; String str2 = "aababcdde"; System.out.println(lettersCommon(str1, str2)); }//end main public static int lettersCommon(String str1, String str2) { int count = 0; // We first sort the given Strings using another method called 'sortString' str1 = sortString(str1); str2 = sortString(str2); // We then remove the repeats within the sorted string using a method called 'removeRepeatLetters' str1 = removeRepeatLetters(str1); str2 = removeRepeatLetters(str2); // For simplicity, we store the lengths of both Strings here int len1 = str1.length(); int len2 = str2.length(); // We count the number of letters in common between both Strings below and return the number of letters in common if(len1 >= len2) { for(int i = 0; i < len2; i++) { for(int j = 0; j < len1; j++) { if(str2.charAt(i) == str1.charAt(j)) count++; } } }else { for(int i = 0; i < len1; i++) { for(int j = 0; j < len2; j++) { if(str1.charAt(i) == str2.charAt(j)) count++; } } } return count; } // Removes repeated letters in String (needs fixing) public static String removeRepeatLetters(String str) { // 'result' is used to store the String w/o repeated letters String strNoRepeat = ""; // Length of String given to method int len = str.length(); // Removing repeated letters from str1 for(int i=0; i < len-1; i++) { if(!(str.charAt(i) == str.charAt(i+1))) { strNoRepeat += str.charAt(i); if(i==len-2 && (!(str.charAt(len-2) == str.charAt(len-1)))) strNoRepeat += str.substring(len-1); } } System.out.println(strNoRepeat); return strNoRepeat; } // Sorts Strings public static String sortString(String inputString) { char tempArray[] = inputString.toCharArray(); Arrays.sort(tempArray); return String.valueOf(tempArray); } }//end class