Skip to content

Commit

Permalink
the first problem from Microsoft
Browse files Browse the repository at this point in the history
  • Loading branch information
dombroks committed Sep 21, 2020
1 parent 21aab06 commit e6b9674
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Microsoft_Problems/Microsoft_Problem_00.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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));

}
}

0 comments on commit e6b9674

Please sign in to comment.