Description

Given a set of strings which just has lower case letters and a target string, output all the strings for each the edit distance with the target no greater than k.

You have the following 3 operations permitted on a word:

  • Insert a character
  • Delete a character
  • Replace a character

Example

Example 1:

Given words = `["abc", "abd", "abcd", "adc"]` and target = `"ac"`, k = `1`
Return `["abc", "adc"]`
Input:
["abc", "abd", "abcd", "adc"]
"ac"
1
Output:
["abc","adc"] Explanation:
"abc" remove "b"
"adc" remove "d"

Example 2:

Input:
["acc","abcd","ade","abbcd"]
"abc"
2
Output:
["acc","abcd","ade","abbcd"] Explanation:
"acc" turns "c" into "b"
"abcd" remove "d"
"ade" turns "d" into "b" turns "e" into "c"
"abbcd" gets rid of "b" and "d"

思路:滚动数组。用字典树对dfs进行优化。
class TrieNode{
public TrieNode[] sons;
public boolean isWord;
public String word; public TrieNode() {
int i;
sons = new TrieNode[26];
for (i = 0; i < 26; ++i) {
sons[i] = null;
} isWord = false;
} static public void Insert(TrieNode p, String word) {
int i;
char[] s = word.toCharArray();
for (i = 0; i < s.length; ++i) {
int c = s[i] - 'a';
if (p.sons[c] == null) {
p.sons[c] = new TrieNode();
} p = p.sons[c];
} p.isWord = true;
p.word = word;
}
} public class Solution {
/**
* @param words: a set of stirngs
* @param target: a target string
* @param k: An integer
* @return: output all the strings that meet the requirements
*/ int K;
int n;
char[] target;
List<String> res; // p is the current TrieNode
// f[] representss f[Sp][...]
void dfs(TrieNode p, int[] f) {
int[] newf;
int i;
if (p.isWord && f[n] <= K) {
res.add(p.word);
} for (int c = 0; c < 26; ++c) {
if (p.sons[c] == null) {
continue;
} // calc newf
newf = new int[n + 1];
// newf[...]: f[Sp + c][....] // newf[j] = Math.min(Math.min(f[j], newf[j-1]), f[j-1]) + 1;
for (i = 0; i <= n; ++i) {
newf[i] = f[i] + 1;
} for (i = 1; i <= n; ++i) {
newf[i] = Math.min(newf[i], f[i - 1] + 1);
} for (i = 1; i <= n; ++i) {
if (target[i - 1] - 'a' == c) {
newf[i] = Math.min(newf[i], f[i - 1]);
} newf[i] = Math.min(newf[i - 1] + 1, newf[i]);
} dfs(p.sons[c], newf);
}
} public List<String> kDistance(String[] words, String targets, int k) {
res = new ArrayList<String>();
K = k;
TrieNode root = new TrieNode();
int i;
for (i = 0; i < words.length; ++i) {
TrieNode.Insert(root, words[i]);
} target = targets.toCharArray();
n = target.length;
int[] f = new int[n + 1];
for (i = 0; i <= n; ++i) {
f[i] = i;
} dfs(root, f);
return res;
}
}

  

K Edit Distance的更多相关文章

  1. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

  2. Min Edit Distance

    Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...

  3. 利用编辑距离(Edit Distance)计算两个字符串的相似度

    利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...

  4. Minimum edit distance(levenshtein distance)(最小编辑距离)初探

    最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...

  5. LeetCode解题报告—— N-Queens && Edit Distance

    1. N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  6. LeetCode(72) Edit Distance

    题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...

  7. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  8. [LeetCode] Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. Edit Distance

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

随机推荐

  1. android 8.0 以后 uiautomator 无法直接使用的问题

    android8.1以后sdk tools自带的uiautomator直接打开,截取不到机器界面信息. 可以使用以下方法手动截取. 首先操作机器定位到要分析的界面. 1.截取uix资源文件 adb s ...

  2. c++ map容器使用及问题

    C++ STL库map容器一些总结,欢迎大家指正补充. map容器由两部分组成,分别为关键字(Key)和值(Value),关键字和值都可以声明为任意类型的数据,注意:关键字唯一,不能重复!使用需包含头 ...

  3. 链表习题(6)-链表返回倒数第k个数的位置的值

    /*链表返回倒数第k个数的位置的值*/ /* 算法思想:先取得链表的长度len,之后获取len-k+1的位置元素的值 */ Elemtype Getelem_rear(LinkList L, int ...

  4. macbook下使用pycharm2019版本配置远程连接服务器

    pycharm提供了很方便的与服务器同步代码,并执行的插件.我在配置windows版的pycharm时配置成功,在挪用到mac上则遇到了些许问题,终于是解决了,在此记录配置的过程 目的:pycharm ...

  5. python 操作redis集群

    一.连接redis集群 python的redis库是不支持集群操作的,推荐库:redis-py-cluster,一直在维护.还有一个rediscluster库,看GitHub上已经很久没更新了. 安装 ...

  6. SpringBoot +MSSQL

    ____SpringBoot +MSSQL_______________________________________________________________________________ ...

  7. mabatis缓存

    一级缓存 public static SqlSession getSqlSession() { String resource = "mybatis-config.xml"; In ...

  8. NIO开发Http服务器(4):Response封装和响应

    最近学习了Java NIO技术,觉得不能再去写一些Hello World的学习demo了,而且也不想再像学习IO时那样编写一个控制台(或者带界面)聊天室.我们是做WEB开发的,整天围着tomcat.n ...

  9. 作为消费者访问提供者提供的功能(eureka的铺垫案例)

    1. 实体类.提供者的创建如本随笔者的Euraka适合初学者的简单小demo中有所展示 2. 创建子工程作为消费者 (1) 添加依赖:切记引入实体类的依赖 <dependencies> & ...

  10. 【转载】C#使用Random类来生成指定范围内的随机数

    C#的程序应用的开发中,可以使用Random随机数类的对象来生成相应的随机数,通过Random随机数对象生成随机数的时候,支持设置随机数的最小值和最大值,例如可以指定生成1到1000范围内的随机数.R ...