K Edit Distance
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的更多相关文章
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- Min Edit Distance
Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Ti ...
- 利用编辑距离(Edit Distance)计算两个字符串的相似度
利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...
- Minimum edit distance(levenshtein distance)(最小编辑距离)初探
最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...
- 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 ...
- LeetCode(72) Edit Distance
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to wo ...
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- 2019秋季PAT甲级_C++题解
2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...
- WordPress实现中英文数字之间自动加空格排版
通常来说中文与英文.中文和数字之间加上空格的排版会更加好看,但是如果让我们在编辑文章的时候人工添加,感觉非常繁琐和让人厌烦,所以今天龙笑天下就来跟大家介绍一下WordPress如何实现中英文数字之间自 ...
- CSS中position 和float的使用说明(清晰)
当构建页面排版时,有不同的方法可以使用.使用哪一种方法取决于具体页面的排版要求,在不同的情况下,某些方法可能好过于其他的方法. 比如,可以使用若干个浮动元素来构建一个整洁简洁的页面排版.或者,如果需要 ...
- Django的Xadmin使用
Django Xadmin 通常在实际的开发当中, 除了前后端分离的项目, 还有一些前后端不分离的项目, 这样我们在访问不分离的页面的时候, 就可以通过Django自带的admin管理模块来轻松实现后 ...
- docker(三):服务services
docker中services位于container上面,services可以控制image的运行方式,包括image运行时所需资源的大小 创建yml文件 yml文件定义了容器运行时的行为.我们先创建 ...
- opencv学习笔记D01
目录 opencv学习笔记D01 一.图片读取 二.图片保存 三.图片展示 四.图片缩放 五.四种常用插值方式的比较 1.最近邻插值 2.双线性插值 3.区域插值 4.三次样条插值 我是尾巴: ope ...
- promethus监控mysql
一.mysqld_exporter安装 下载页面 https://github.com/prometheus/mysqld_exporter/releases 下载最新版本 https://githu ...
- Locust性能测试_百度案例
一.安装: 1.Locust在PyPI上可用,可以通过pip或easy_install安装:pip install locustio 2.查看Locust可用选项:loc ...
- SQL Server中,常用的全局变量
在SQL Server中,全局变量是一种特殊类型的变量,服务器将维护这些变量的值.全局变量以@@前缀开头,不必进行声明,它们属于系统定义的函数.下表就是SQL Server中一些常用的全局变量. 全局 ...
- Spring AOP创建BeforeAdvice和AfterAdvice实例
BeforeAdvice 1.会在目标对象的方法执行之前被调用. 2.通过实现MethodBeforeAdvice接口来实现. 3.该接口中定义了一个方法即before方法,before方法会在目标对 ...