Skip to content

Commit 546bc3f

Browse files
authored
Add New String Interleave Class and Tests (TheAlgorithms#2032)
* added an Interleave class * added interleaving sequence url * Fixes: TheAlgorithms#2031 Co-authored-by: u6943702 <[email protected]>
1 parent 4591884 commit 546bc3f

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package com.string;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class Interleave {
11+
@Test
12+
public void testInterleaveRegularString() {
13+
String string1 = "Hello";
14+
String string2 = "World";
15+
String expected = "HWeolrllod";
16+
String actual = interleave(string1, string2);
17+
assertEquals("Incorrect result from method.", expected, actual);
18+
}
19+
20+
@Test
21+
public void testInterleaveEmptyString() {
22+
String string1 = "";
23+
String string2 = "";
24+
String string3 = "a";
25+
String string4 = "abc";
26+
String expected1 = "";
27+
String actual1 = interleave(string1, string2);
28+
String expected2 = "a";
29+
String actual2 = interleave(string1, string3);
30+
String expected3 = "abc";
31+
String actual3 = 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+
public void testInterleaveSingleString() {
39+
String string1 = "a";
40+
String string2 = "b";
41+
String expected = "ab";
42+
String actual = interleave(string1, string2);
43+
assertEquals("Incorrect result from method.", expected, actual);
44+
}
45+
46+
@Test
47+
public void testInterleaveIntString() {
48+
String string1 = "1";
49+
String string2 = "7";
50+
String expected = "17";
51+
String actual = interleave(string1, string2);
52+
assertEquals("Incorrect result from method.", expected, actual);
53+
}
54+
55+
@Test
56+
public void testInterleaveMixedString() {
57+
String string1 = "1a2b3c4d";
58+
String string2 = "5e6f7g8h";
59+
String expected = "15ae26bf37cg48dh";
60+
String actual = interleave(string1, string2);
61+
assertEquals("Incorrect result from method.", expected, actual);
62+
}
63+
64+
@Test
65+
public void testInterleaveSymbols() {
66+
String string1 = "a@b%c/";
67+
String string2 = "d#e$g%.";
68+
String expected = "ad@#be%$cg/%.";
69+
String actual = interleave(string1, string2);
70+
assertEquals("Incorrect result from method.", expected, actual);
71+
}
72+
73+
@Test
74+
public void testInterleaveSpaces() { // This string interleave algorithm defines a space as a valid character.
75+
String string1 = " ";
76+
String string2 = "a";
77+
String string3 = "5 g";
78+
String string4 = " 4 d ";
79+
String expected1 = " a";
80+
String actual1 = interleave(string1, string2);
81+
String expected2 = "a5 g";
82+
String actual2 = interleave(string2, string3);
83+
String expected3 = "5 4g d ";
84+
String actual3 = 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+
public String interleave(String string1, String string2) {
105+
String result = ""; // The final interleaved string to return.
106+
List<Character> list1 = new ArrayList<>(); // The ArrayList of string1, with each character being an individual element.
107+
List<Character> list2 = new ArrayList<>(); // The ArrayList of string2, in a similar manner as above.
108+
109+
for (int i = 0; i < string1.length(); i++) // Convert string1 into list1.
110+
list1.add(string1.charAt(i));
111+
112+
for (int i = 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 (int j = 0; j < list1.size(); j++) {
117+
result = result + list1.get(j);
118+
result = result + list2.get(j);
119+
}
120+
return result;
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 (char character : list1) { // Concatenate the rest of the characters in list1 to the result.
131+
result = result + character;
132+
}
133+
return result;
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 (char character : list2) { // Concatenate the rest of the characters in list2 to the result.
144+
result = result + character;
145+
}
146+
return result;
147+
}
148+
149+
return result;
150+
}
151+
}

0 commit comments

Comments
 (0)