-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicrosoft_Problem_00.java
46 lines (33 loc) · 1.61 KB
/
Microsoft_Problem_00.java
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
/*
This problem was asked by Microsoft.
Given a dictionary of words and a string made up of those words (no spaces), return the original sentence in a list. If there is more than one possible reconstruction, return any of them. If there is no possible reconstruction, then return null.
For example, given the set of words 'quick', 'brown', 'the', 'fox', and the string "thequickbrownfox", you should return ['the', 'quick', 'brown', 'fox'].
Given the set of words 'bed', 'bath', 'bedbath', 'and', 'beyond', and the string "bedbathandbeyond", return either ['bed', 'bath', 'and', 'beyond] or ['bedbath', 'and', 'beyond'].
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Microsoft_Problem_00 {
public static List<String> getOriginalSentence(String[] dict, String sentence) {
Map map = new HashMap();
List<String> original = new ArrayList<>();
for (String word : dict) {
if (sentence.contains(word)) {
map.put(sentence.indexOf(word), word);
}
}
for (Object i : map.values()) {
original.add(i.toString());
}
return original;
}
public static void main(String[] args) {
String[] dict = new String[]{"bed", "bath", "bedbath", "and", "beyond"};
String sentence = "bedbathandbeyond";
String[] dict2 = new String[]{"quick", "brown", "the", "fox"};
String sentence2 = "thequickbrownfox";
System.out.println(getOriginalSentence(dict, sentence));
System.out.println(getOriginalSentence(dict2, sentence2));
}
}