通行证
- 积分
- 600
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-8-10
- 最后登录
- 1970-1-1
|
没有直接的题,上个solution- import java.util.*;
- public class Ana {
- // dict ["eat","tea","nat","bat"]
- // input ["ate", "tan"]
- List<List<String>> getAna(String[] input, String[] dict) {
- List<List<String>> result = new ArrayList<>();
- // setup map
- Map<String, List<String>> map = new HashMap<>();
- for (String ele : dict) {
- char[] arr = ele.toCharArray();
- Arrays.sort(arr);
- String key = new String(arr);
- if (!map.containsKey(key)) {
- map.put(key, new ArrayList<>());
- }
- map.get(key).add(ele);
- }
- List<String> curr = new ArrayList<>();
- helper(input, map, result, new ArrayList<>(), 0);
- return result;
- }
- private void helper(String[] input, Map<String,
- List<String>> map, List<List<String>> result, List<String> current, int idx) {
- if (idx == input.length) {
- result.add(new ArrayList<>(current));
- return;
- }
- // find possible replacement
- String now = input[idx];
- char[] temp = now.toCharArray();
- Arrays.sort(temp);
- String key = new String(temp);
- if (!map.containsKey(key)) {
- current.add(now);
- helper(input, map, result, current, idx + 1);
- current.remove(current.size() - 1);
- } else {
- for (String ele : map.get(key)) {
- current.add(ele);
- helper(input, map, result, current, idx + 1);
- current.remove(current.size() - 1);
- }
- }
- }
- }
复制代码 |
|