|
| 1 | +packagecom.string; |
| 2 | + |
| 3 | +importorg.junit.Test; |
| 4 | + |
| 5 | +importjava.util.ArrayList; |
| 6 | +importjava.util.List; |
| 7 | + |
| 8 | +importstaticorg.junit.Assert.assertEquals; |
| 9 | + |
| 10 | +publicclassInterleave { |
| 11 | +@Test |
| 12 | +publicvoidtestInterleaveRegularString() { |
| 13 | +Stringstring1 ="Hello"; |
| 14 | +Stringstring2 ="World"; |
| 15 | +Stringexpected ="HWeolrllod"; |
| 16 | +Stringactual =interleave(string1,string2); |
| 17 | +assertEquals("Incorrect result from method.",expected,actual); |
| 18 | + } |
| 19 | + |
| 20 | +@Test |
| 21 | +publicvoidtestInterleaveEmptyString() { |
| 22 | +Stringstring1 =""; |
| 23 | +Stringstring2 =""; |
| 24 | +Stringstring3 ="a"; |
| 25 | +Stringstring4 ="abc"; |
| 26 | +Stringexpected1 =""; |
| 27 | +Stringactual1 =interleave(string1,string2); |
| 28 | +Stringexpected2 ="a"; |
| 29 | +Stringactual2 =interleave(string1,string3); |
| 30 | +Stringexpected3 ="abc"; |
| 31 | +Stringactual3 =interleave(string1,string4); |
| 32 | +assertEquals("Incorrect result from method.",expected1,actual1); |
| 33 | +assertEquals("Incorrect result from method.",expected2,actual2); |
| 34 | +assertEquals("Incorrect result from method.",expected3,actual3); |
| 35 | + } |
| 36 | + |
| 37 | +@Test |
| 38 | +publicvoidtestInterleaveSingleString() { |
| 39 | +Stringstring1 ="a"; |
| 40 | +Stringstring2 ="b"; |
| 41 | +Stringexpected ="ab"; |
| 42 | +Stringactual =interleave(string1,string2); |
| 43 | +assertEquals("Incorrect result from method.",expected,actual); |
| 44 | + } |
| 45 | + |
| 46 | +@Test |
| 47 | +publicvoidtestInterleaveIntString() { |
| 48 | +Stringstring1 ="1"; |
| 49 | +Stringstring2 ="7"; |
| 50 | +Stringexpected ="17"; |
| 51 | +Stringactual =interleave(string1,string2); |
| 52 | +assertEquals("Incorrect result from method.",expected,actual); |
| 53 | + } |
| 54 | + |
| 55 | +@Test |
| 56 | +publicvoidtestInterleaveMixedString() { |
| 57 | +Stringstring1 ="1a2b3c4d"; |
| 58 | +Stringstring2 ="5e6f7g8h"; |
| 59 | +Stringexpected ="15ae26bf37cg48dh"; |
| 60 | +Stringactual =interleave(string1,string2); |
| 61 | +assertEquals("Incorrect result from method.",expected,actual); |
| 62 | + } |
| 63 | + |
| 64 | +@Test |
| 65 | +publicvoidtestInterleaveSymbols() { |
| 66 | +Stringstring1 ="a@b%c/"; |
| 67 | +Stringstring2 ="d#e$g%."; |
| 68 | +Stringexpected ="ad@#be%$cg/%."; |
| 69 | +Stringactual =interleave(string1,string2); |
| 70 | +assertEquals("Incorrect result from method.",expected,actual); |
| 71 | + } |
| 72 | + |
| 73 | +@Test |
| 74 | +publicvoidtestInterleaveSpaces() {// This string interleave algorithm defines a space as a valid character. |
| 75 | +Stringstring1 =" "; |
| 76 | +Stringstring2 ="a"; |
| 77 | +Stringstring3 ="5 g"; |
| 78 | +Stringstring4 =" 4 d "; |
| 79 | +Stringexpected1 =" a"; |
| 80 | +Stringactual1 =interleave(string1,string2); |
| 81 | +Stringexpected2 ="a5 g"; |
| 82 | +Stringactual2 =interleave(string2,string3); |
| 83 | +Stringexpected3 ="5 4g d "; |
| 84 | +Stringactual3 =interleave(string3,string4); |
| 85 | +assertEquals("Incorrect result from method.",expected1,actual1); |
| 86 | +assertEquals("Incorrect result from method.",expected2,actual2); |
| 87 | +assertEquals("Incorrect result from method.",expected3,actual3); |
| 88 | + } |
| 89 | + |
| 90 | +/** |
| 91 | + * This method "interweaves" two input strings one character at a time. The first character of the |
| 92 | + * first parameter string always starts the resulting string, unless that character is a space or is empty. |
| 93 | + * This string interleaving method takes a space in a string (e.g. " ") into consideration. |
| 94 | + * |
| 95 | + * For example, if string1 = "abc" and string2 = "def", then the result would be "adbecf", as the first character |
| 96 | + * of the string1 is 'a', then the first character of string2 is 'd', and so forth. |
| 97 | + * |
| 98 | + * For more information on interleaving, check out: https://en.wikipedia.org/wiki/Interleave_sequence |
| 99 | + * |
| 100 | + * @param string1 |
| 101 | + * @param string2 |
| 102 | + * @return string resulting from the interweaving of the two input strings; string1 and string2. |
| 103 | + */ |
| 104 | +publicStringinterleave(Stringstring1,Stringstring2) { |
| 105 | +Stringresult ="";// The final interleaved string to return. |
| 106 | +List<Character>list1 =newArrayList<>();// The ArrayList of string1, with each character being an individual element. |
| 107 | +List<Character>list2 =newArrayList<>();// The ArrayList of string2, in a similar manner as above. |
| 108 | + |
| 109 | +for (inti =0;i <string1.length();i++)// Convert string1 into list1. |
| 110 | +list1.add(string1.charAt(i)); |
| 111 | + |
| 112 | +for (inti =0;i <string2.length();i++)// Convert string2 into list2. |
| 113 | +list2.add(string2.charAt(i)); |
| 114 | + |
| 115 | +if (string1.length() ==string2.length()) {// Interleaving when string1 and string2 are equal length. |
| 116 | +for (intj =0;j <list1.size();j++) { |
| 117 | +result =result +list1.get(j); |
| 118 | +result =result +list2.get(j); |
| 119 | + } |
| 120 | +returnresult; |
| 121 | + } |
| 122 | + |
| 123 | +if (string1.length() >string2.length()) {// Interleaving when string1 is longer than string2. |
| 124 | +while (list2.size() >0) { |
| 125 | +result =result +list1.get(0); |
| 126 | +list1.remove(0); |
| 127 | +result =result +list2.get(0); |
| 128 | +list2.remove(0); |
| 129 | + } |
| 130 | +for (charcharacter :list1) {// Concatenate the rest of the characters in list1 to the result. |
| 131 | +result =result +character; |
| 132 | + } |
| 133 | +returnresult; |
| 134 | + } |
| 135 | + |
| 136 | +if (string2.length() >string1.length()) {// Interleaving when string2 is longer than string1. |
| 137 | +while (list1.size() >0) { |
| 138 | +result =result +list1.get(0); |
| 139 | +list1.remove(0); |
| 140 | +result =result +list2.get(0); |
| 141 | +list2.remove(0); |
| 142 | + } |
| 143 | +for (charcharacter :list2) {// Concatenate the rest of the characters in list2 to the result. |
| 144 | +result =result +character; |
| 145 | + } |
| 146 | +returnresult; |
| 147 | + } |
| 148 | + |
| 149 | +returnresult; |
| 150 | + } |
| 151 | +} |