-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunionStrings.java
More file actions
59 lines (53 loc) · 2.09 KB
/
unionStrings.java
File metadata and controls
59 lines (53 loc) · 2.09 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
/* Write a function my_union that takes two strings and returns, without doubles,
* the characters that appear in either one of the strings.
* Input: "zpadinton" && "paqefwtdjetyiytjneytjoeyjnejeyj"
* Output:
* Return Value: "zpadintoqefwjy"
*
* Iterate over first string, check if char is in the DS.
* If not, add to it.
* Same with second string
* Convert output to string and return
* DS needs to maintain insertion order and be searchable
*/
import java.util.*;
public class unionStrings {
public static String my_union(String str1, String str2) {
ArrayList<Character> uniqueChars = new ArrayList<>();
for (char letter : str1.toCharArray()) {
if (!uniqueChars.contains(letter)) {
uniqueChars.add(letter);
}
}
for (char letter : str2.toCharArray()) {
if (!uniqueChars.contains(letter)) {
uniqueChars.add(letter);
}
}
// System.out.println("24uniqueChars AL: " + uniqueChars);
StringBuilder uniqueSB = new StringBuilder();
for (char letter : uniqueChars) {
uniqueSB.append(letter);
}
// System.out.println("34uniqueSB2str: " + uniqueSB.toString());
return uniqueSB.toString();
}
public static void main(String[] args) {
String input1a = "zpadinton";
String input1b = "paqefwtdjetyiytjneytjoeyjnejeyj";
// my_union(input1a, input1b);
String expected1 = "zpadintoqefwjy";
boolean compare1 = expected1.equals(my_union(input1a, input1b));
System.out.println(compare1);
String input2a = "ddf6vewg64f";
String input2b = "gtwthgdwthdwfteewhrtag6h4ffdhsd";
String expected2 = "df6vewg4thras";
boolean compare2 = expected2.equals(my_union(input2a, input2b));
System.out.println(compare2);
String input3a = "rien";
String input3b = "cette phrase ne cache rien";
String expected3 = "rienct phas";
boolean compare3 = expected3.equals(my_union(input3a, input3b));
System.out.println(compare3);
}
}