najimCseJu

OPT01

Sep 22nd, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. String[] values = new String[]{"---", ".--.", "-", "..", "...-", ".", ".-."};
  2.     Map<String, Character> map = new HashMap<>();
  3.     List<String> ans = new ArrayList<>();
  4.  
  5.     public List<String> decode(String code) {
  6.         for (int i = 0; i < "OPTIVER".length(); i++) {
  7.             map.put(values[i], "OPTIVER".charAt(i));
  8.         }
  9.         ans.clear();
  10.         backtrack(0, "", code);
  11.         return ans;
  12.  
  13.     }
  14.  
  15.     private void backtrack(int pos, String res, String code) {
  16.         if (pos == code.length()) {
  17.             ans.add(res);
  18.             return;
  19.         }
  20.         int mx = Math.min(pos + 4, code.length());
  21.         for (int i = pos + 1; i <= mx; i++) {
  22.             String key = code.substring(pos, i);
  23.             if (map.containsKey(key)) {
  24.                 backtrack(i, res + map.get(key), code);
  25.             }
  26.         }
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment