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

Posted apanda009

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于find all palidrome string by deleting any letter from the given string的主要内容,如果未能解决你的问题,请参考以下文章

Find all the permutations of a string

438. Find All Anagrams in a String

LeetCode - Find All Anagrams in a String

438. Find All Anagrams in a String

438. Find All Anagrams in a String

438. Find All Anagrams in a String