K Edit Distance

80 篇文章 0 订阅
16 篇文章 0 订阅

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"

思路:用trie来建立字典树,然后在字典树里面用dfs来写动态规划,原来本来是二维的,现在用dfs来表示一维,在节点上建立int数组来表示第二维度。dp[i] 表示的物理意义是:从trie的root走到当前节点,形成的prefix,和target的前i个字符的最小编辑距离;

注意: next[0] = dp[0] + 1; 很重要,这里相当于for循环的initial里面的col initial,target j不变,a字符串的edit distance;

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
     */
    public List<String> kDistance(String[] words, String target, int k) {
        List<String> list = new ArrayList<String>();
        if(words == null || words.length == 0 || target == null || k < 0) {
            return list;
        }
        
        // build trie, and insert word;
        Trie trie = new Trie();
        for(int i = 0; i < words.length; i++){
            trie.insert(words[i]);
        }
        
        char[] targetChars = target.toCharArray();
        // f[root][0...n];
        // f[''][0...n];
        int n = target.length();
        int[] f = new int[n+1];
        for(int i = 0; i <=n; i++) {
            f[i] = i;
        }
        
        dfs(trie.root, targetChars, k, list, f);
        return list;
    }
    
    private void dfs(TrieNode node, char[] targetChars, 
                        int k, List<String> list, int[] dp) {
        int n = targetChars.length;
        if(node.isword && dp[n] <= k){
            list.add(node.word);
        }
        
        // dp[i] 表示的物理意义是:从trie的root走到当前节点,形成的prefix
        // 和target的前i个字符的最小编辑距离;
        int[] next = new int[n+1];
        for(int i = 0; i < next.length; i++) {
            next[i] = 0;
        }
        
        for(int i = 0; i < 26; i++) {
            if(node.sons[i] == null) {
                continue;
            }
            
            //这里一步很重要,next[0] = dp[0] + 1;
            //这里代表trie又走了一步,但是j还是0,不变;那么edit distance+1;
            next[0] = dp[0] + 1; 
            for(int j = 1; j <= n; j++) {
                if('a' + i == targetChars[j-1]) {
                    next[j] = dp[j-1];
                } else {
                    next[j] = Math.min(dp[j-1] + 1, 
                                Math.min(next[j-1] + 1, dp[j] + 1));
                }
            }
            dfs(node.sons[i], targetChars, k, list, next);
        }
    }
    
    private class TrieNode {
        public TrieNode[] sons;
        public boolean isword;
        public String word;
        
        public TrieNode() {
            sons = new TrieNode[26];
            for(int i = 0; i < 26; i++) {
                sons[i] = null;
            }
            this.isword = false;
            this.word = null;
        }
    }
    
    private class Trie {
        public TrieNode root;
        public Trie () {
            root = new TrieNode();
        }
        
        public void insert(String word) {
            if(word == null) return;
            TrieNode cur = root;
            char[] s = word.toCharArray();
            
            for(int i = 0; i < s.length; i++) {
                int c = s[i] - 'a';
                if(cur.sons[c] == null) {
                    cur.sons[c] = new TrieNode();
                }
                cur = cur.sons[c];
            }
            cur.isword = true;
            cur.word = word;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值