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. Linux下的JMeter部署及使用

    之前都是在windows环境使用JMeter,是有操作界面的.但是最近需要在Linux环境下使用,现将操作步骤记录下来 在安装JMeter之前,需要在Linux下安装JDK并配置环境变量,这里跳过 1 ...

  2. python中将已有链接的视频进行下载

    使用python爬取视频网站时,会得到一系列的视频链接,比如MP4文件.得到视频文件之后需要对视频进行下载,本文写出下载视频文件的函数. 首先导入requests库,安装库使用pip install ...

  3. Java 总结篇2

    第02章:数据类型和运算符 一.概述: 1.数据类型:int.float.char.boolean 2.运算符:算术运算符.赋值运算符.关系运算符.逻辑运算符.位运算符(了解即可).条件运算符 3.基 ...

  4. 【Linux】一步一步学Linux——Centos7.5安装图解(08)

    00. 目录 参考博客:https://mp.csdn.net/mdeditor/95031775# 01. Centos7.5简介 CentOS(Community Enterprise Opera ...

  5. A Simple Question of Chemistry

    #include<stdio.h> int main() { int i, l; ]; ]; l = ; ) { l++; } ; a[i]!= && i<l; i+ ...

  6. Spring AOP 创建Advice 基于Annotation

    public interface IHello { public void sayHello(String str); } public class Hello implements IHello { ...

  7. 使用PrintDocument定制打印格式

    虽然说使在IE上直接调用打印插件打印已经不常用,但是有时候还是会用到,这里就记录一下. 首先我们列出来我们的打印类 public class PrintService { //打印机名称 privat ...

  8. 一张图看懂SharpImage

    通过下面的图片,可以瞬间看懂整个类库的脉络.图片比较大,如果看不清,可

  9. OO第三单元(地铁,JML)单元总结

    OO第三单元(地铁,JML)单元总结 这是我们OO课程的第二个单元,这个单元的主要目的是让我们熟悉并了解JML来是我们具有规格化编程架构的思想.这个单元的主题一开始并不明了,从第一次作业的路径到第二次 ...

  10. kubernetes第五章--创建资源的两种方式