Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String[] values = new String[]{"---", ".--.", "-", "..", "...-", ".", ".-."};
- Map<String, Character> map = new HashMap<>();
- List<String> ans = new ArrayList<>();
- public List<String> decode(String code) {
- for (int i = 0; i < "OPTIVER".length(); i++) {
- map.put(values[i], "OPTIVER".charAt(i));
- }
- ans.clear();
- backtrack(0, "", code);
- return ans;
- }
- private void backtrack(int pos, String res, String code) {
- if (pos == code.length()) {
- ans.add(res);
- return;
- }
- int mx = Math.min(pos + 4, code.length());
- for (int i = pos + 1; i <= mx; i++) {
- String key = code.substring(pos, i);
- if (map.containsKey(key)) {
- backtrack(i, res + map.get(key), code);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment