当前位置: 首页 > 工具软件 > Strings edit > 使用案例 >

K Edit Distance

宋翔
2023-12-01

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;
        }
    }
}

 

 类似资料:

相关阅读

相关文章

相关问答