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 ...
随机推荐
- setInterval定时器停止后,再重新启动
1.数据自动滚动显示(动态添加) <li> <div class="FULeTi"> <div class="SLeName"&g ...
- Java开发笔记(一百零八)JSON串的定义和解析
前面提到URL尾巴支持添加请求参数,具体格式形如“参数A名称=A参数值&参数B名称=B参数值”,可是这种格式只能传递简单的键值对信息,不能传递结构化数据,也无法传递数组形式的参数,因而它不适用 ...
- python学习-38迭代器和生成器
迭代器和生成器 ---- 迭代器协议和for循环工作机制 1.迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么引起一个Stoplteration异常,以终止迭代(只能往 ...
- day28——C/S与B/S架构、网络通信原理、osi七层协议、UDP、TCP协议、TCP的三次握手与四次挥手
day28 C/S B/S架构 C:client 客户端 B:browse浏览器 S:server 服务端 C/S C/S架构:基于客户端与服务端之间的通信 QQ.游戏.皮皮虾 优点:个性化设 ...
- pandas再次学习
numpy.scipy官方文档 pandas官方网站 matplotlib官方文档 一.数据结构 二.数据处理 1.数据获取(excel文件数据基本信息) #coding=utf-8 import ...
- linux中用户环境变量问题
修改所有用户的环境变量:/etc/profile文件 只修改root用户的环境变量:~/.bashrc文件 只修改某个非root用户的环境变量:/home/非root用户名/.bashrc文件
- 笨办法学python 习题14 优化过 遇到问题的请看
print "\t what's you name?"user_name = raw_input('>') from sys import argvscript, = arg ...
- 使用PHP开发HR系统(1)
本文通过笔者的实践,讲述如何以PHP+CI+Postgres构建一套人力资源管理系统. ======================================================== ...
- CCF 2016-04-1 折点计数
CCF 2016-04-1 折点计数 题目 问题描述 给定n个整数表示一个商店连续n天的销售量.如果某天之前销售量在增长,而后一天销售量减少,则称这一天为折点,反过来如果之前销售量减少而后一天销售量增 ...
- Linux中shell字符串分隔、字符串替换、字符串拼接
1.从properties文件中读取变量 SERVER_NAME=`sed '/project.config/!d;s/.*=//' conf/dubbo.properties | tr -d '\r ...