This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathCombineTwoStrings.java
More file actions
75 lines (70 loc) · 2.28 KB
/
Copy pathCombineTwoStrings.java
File metadata and controls
75 lines (70 loc) · 2.28 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
import java.util.HashMap;
import java.util.Stack;
/**
*
* One possible solution is to concatenate first and second string, then find
* all possible anagrams of that string, and finally check if the third string
* is present in one of the anagrams. Works! But the problem is generating the
* anagrams. Deteriorates the complexity.
*
* Another solution is to compromise of the space complexity a little, and make
* use of a stack as below.
*
* Addition: We could also use a HashMap to record the rank of each character
* in the combined string and check the two input strings as below.
*/
public class CombineTwoStrings {
public static void main(String[] args) {
CombineTwoStrings cts = new CombineTwoStrings();
String one = "rohit";
String two = "deepthi";
String three = "viveka";
String combined = "rodehepitht";
String combined2 = "rviovehkiat";
System.out.println(cts.isValid(one, two, combined));
System.out.println(cts.isValidTwo(one, three, combined2));
}
public boolean isValid(String one, String two, String combined) {
Stack<Character> s = new Stack<Character>();
if ((one.length() + two.length()) != combined.length())
return false;
for (int i = combined.length() - 1; i >= 0; i--) {
s.push(combined.charAt(i));
}
int oneIndex = 0;
int twoIndex = 0;
while (!s.isEmpty() && oneIndex < one.length()
&& twoIndex < two.length()) {
char comparer = s.pop();
if (comparer == one.charAt(oneIndex))
oneIndex++;
else if (comparer == two.charAt(twoIndex))
twoIndex++;
else
return false;
}
return true;
}
// Used a hashMap to save the rank of each character in the combined string.
// Incrementing the pointer of respective string as per the rank in the
// combined string.
public boolean isValidTwo(String one, String two, String combined) {
if(one.length() + two.length() != combined.length())
return false;
HashMap<Integer, Character> map = new HashMap<>();
int key = 0, p1 = 0, p2 = 0, pT = 0;
for(Character i : combined.toCharArray()) {
map.put(key++, i);
}
while(p1 <= one.length() - 1 && p2 <= two.length() - 1 && pT <= combined.length() - 1) {
char temp = map.get(pT++);
if(one.charAt(p1) == temp)
p1++;
else if (two.charAt(p2) == temp)
p2++;
else
return false;
}
return true;
}
}