forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterleave.java
More file actions
151 lines (134 loc) · 5.79 KB
/
Interleave.java
File metadata and controls
151 lines (134 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.string;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class Interleave {
@Test
public void testInterleaveRegularString() {
String string1 = "Hello";
String string2 = "World";
String expected = "HWeolrllod";
String actual = interleave(string1, string2);
assertEquals("Incorrect result from method.", expected, actual);
}
@Test
public void testInterleaveEmptyString() {
String string1 = "";
String string2 = "";
String string3 = "a";
String string4 = "abc";
String expected1 = "";
String actual1 = interleave(string1, string2);
String expected2 = "a";
String actual2 = interleave(string1, string3);
String expected3 = "abc";
String actual3 = interleave(string1, string4);
assertEquals("Incorrect result from method.", expected1, actual1);
assertEquals("Incorrect result from method.", expected2, actual2);
assertEquals("Incorrect result from method.", expected3, actual3);
}
@Test
public void testInterleaveSingleString() {
String string1 = "a";
String string2 = "b";
String expected = "ab";
String actual = interleave(string1, string2);
assertEquals("Incorrect result from method.", expected, actual);
}
@Test
public void testInterleaveIntString() {
String string1 = "1";
String string2 = "7";
String expected = "17";
String actual = interleave(string1, string2);
assertEquals("Incorrect result from method.", expected, actual);
}
@Test
public void testInterleaveMixedString() {
String string1 = "1a2b3c4d";
String string2 = "5e6f7g8h";
String expected = "15ae26bf37cg48dh";
String actual = interleave(string1, string2);
assertEquals("Incorrect result from method.", expected, actual);
}
@Test
public void testInterleaveSymbols() {
String string1 = "a@b%c/";
String string2 = "d#e$g%.";
String expected = "ad@#be%$cg/%.";
String actual = interleave(string1, string2);
assertEquals("Incorrect result from method.", expected, actual);
}
@Test
public void testInterleaveSpaces() { // This string interleave algorithm defines a space as a valid character.
String string1 = " ";
String string2 = "a";
String string3 = "5 g";
String string4 = " 4 d ";
String expected1 = " a";
String actual1 = interleave(string1, string2);
String expected2 = "a5 g";
String actual2 = interleave(string2, string3);
String expected3 = "5 4g d ";
String actual3 = interleave(string3, string4);
assertEquals("Incorrect result from method.", expected1, actual1);
assertEquals("Incorrect result from method.", expected2, actual2);
assertEquals("Incorrect result from method.", expected3, actual3);
}
/**
* This method "interweaves" two input strings one character at a time. The first character of the
* first parameter string always starts the resulting string, unless that character is a space or is empty.
* This string interleaving method takes a space in a string (e.g. " ") into consideration.
*
* For example, if string1 = "abc" and string2 = "def", then the result would be "adbecf", as the first character
* of the string1 is 'a', then the first character of string2 is 'd', and so forth.
*
* For more information on interleaving, check out: https://en.wikipedia.org/wiki/Interleave_sequence
*
* @param string1
* @param string2
* @return string resulting from the interweaving of the two input strings; string1 and string2.
*/
public String interleave(String string1, String string2) {
String result = ""; // The final interleaved string to return.
List<Character> list1 = new ArrayList<>(); // The ArrayList of string1, with each character being an individual element.
List<Character> list2 = new ArrayList<>(); // The ArrayList of string2, in a similar manner as above.
for (int i = 0; i < string1.length(); i++) // Convert string1 into list1.
list1.add(string1.charAt(i));
for (int i = 0; i < string2.length(); i++) // Convert string2 into list2.
list2.add(string2.charAt(i));
if (string1.length() == string2.length()) { // Interleaving when string1 and string2 are equal length.
for (int j = 0; j < list1.size(); j++) {
result = result + list1.get(j);
result = result + list2.get(j);
}
return result;
}
if (string1.length() > string2.length()) { // Interleaving when string1 is longer than string2.
while (list2.size() > 0) {
result = result + list1.get(0);
list1.remove(0);
result = result + list2.get(0);
list2.remove(0);
}
for (char character : list1) { // Concatenate the rest of the characters in list1 to the result.
result = result + character;
}
return result;
}
if (string2.length() > string1.length()) { // Interleaving when string2 is longer than string1.
while (list1.size() > 0) {
result = result + list1.get(0);
list1.remove(0);
result = result + list2.get(0);
list2.remove(0);
}
for (char character : list2) { // Concatenate the rest of the characters in list2 to the result.
result = result + character;
}
return result;
}
return result;
}
}