find all palidrome string by deleting any letter from the given string

我就想了个递归, 还是没有区分掉一些重复的情况,worst case O(2^n)基本同暴力解
Map<Integer, Set<String>> allSubSet = new HashMap();
Set<String> getAllPalidrome(String s, int x, int y){
  int ind = x * s.length() + y;
  if(allSubSet.constainsKey(ind)) return allSubSet.get(ind);
  Set<String> ret = new HashSet();
  if (s == null || s.size() == 0) { ret.add(""); return ret;}
  if (s.size() == 1) { ret.add(s); return ret;}
  for(int i = x; i <= y; i++){
    for (int j = y; j >= i; j--){
      if (s.charAt(i) == s.charAt(j)){
         Set<String> subSet = getAllPalidrome(s, i + 1, j - 1);
         ret.addAll(subSet);
         for (String str : subSet) ret.add(s.charAt(i) + str + s.charAt(i));
      }
    }
  }
  allSubSet.put(ind, ret);
  return ret;
}

  

posted @ 2017-12-03 07:41  apanda009  阅读(138)  评论(0编辑  收藏  举报