|
| 1 | +package com..houbb.leetcode.topics.editdistance; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class T72_minDistanceList_V1_DP { |
| 7 | + |
| 8 | +public static void main(String[] args) { |
| 9 | +T72_minDistanceList_V1_DP dp = new T72_minDistanceList_V1_DP(); |
| 10 | +System.out.println(dp.minDistance("horse", "ros")); |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +public List<String> minDistance(String word1, String word2) { |
| 15 | +int m = word1.length(); |
| 16 | +int n = word2.length(); |
| 17 | + |
| 18 | +// DP数组,记录最小操作数 |
| 19 | +int[][] dp = new int[m + 1][n + 1]; |
| 20 | +// 操作类型数组,记录操作类型:"" = no-op, "insert", "delete", "replace" |
| 21 | +String[][] operation = new String[m + 1][n + 1]; |
| 22 | + |
| 23 | +// 初始化 |
| 24 | +for (int i = 0; i <= m; i++) { |
| 25 | +dp[i][0] = i; |
| 26 | +operation[i][0] = "delete"; |
| 27 | +} |
| 28 | +for (int j = 0; j <= n; j++) { |
| 29 | +dp[0][j] = j; |
| 30 | +operation[0][j] = "insert"; |
| 31 | +} |
| 32 | + |
| 33 | +// 填充DP表和操作类型表 |
| 34 | +for (int i = 1; i <= m; i++) { |
| 35 | +for (int j = 1; j <= n; j++) { |
| 36 | +if (word1.charAt(i - 1) == word2.charAt(j - 1)) { |
| 37 | +dp[i][j] = dp[i - 1][j - 1]; |
| 38 | +operation[i][j] = ""; // 字符相等,不需要操作 |
| 39 | +} else { |
| 40 | +int insert = dp[i][j - 1] + 1; |
| 41 | +int delete = dp[i - 1][j] + 1; |
| 42 | +int replace = dp[i - 1][j - 1] + 1; |
| 43 | + |
| 44 | +// 选择最小的操作 |
| 45 | +dp[i][j] = Math.min(Math.min(insert, delete), replace); |
| 46 | + |
| 47 | +// 记录操作类型 |
| 48 | +if (dp[i][j] == insert) { |
| 49 | +operation[i][j] = "insert"; |
| 50 | +} else if (dp[i][j] == delete) { |
| 51 | +operation[i][j] = "delete"; |
| 52 | +} else { |
| 53 | +operation[i][j] = "replace"; |
| 54 | +} |
| 55 | +} |
| 56 | +} |
| 57 | +} |
| 58 | + |
| 59 | +// 回溯操作路径,生成变换过程 |
| 60 | +List<String> result = new ArrayList<>(); |
| 61 | +int i = m, j = n; |
| 62 | +StringBuilder currentWord = new StringBuilder(word1); |
| 63 | + |
| 64 | +// 回溯路径 |
| 65 | +while (i > 0 || j > 0) { |
| 66 | +addNewWord(result, currentWord.toString()); |
| 67 | +if (operation[i][j].equals("")) { |
| 68 | +i--; |
| 69 | +j--; |
| 70 | +} else if (operation[i][j].equals("insert")) { |
| 71 | +currentWord.insert(i, word2.charAt(j - 1)); |
| 72 | +j--; |
| 73 | +} else if (operation[i][j].equals("delete")) { |
| 74 | +currentWord.deleteCharAt(i - 1); |
| 75 | +i--; |
| 76 | +} else if (operation[i][j].equals("replace")) { |
| 77 | +currentWord.setCharAt(i - 1, word2.charAt(j - 1)); |
| 78 | +i--; |
| 79 | +j--; |
| 80 | +} |
| 81 | +} |
| 82 | + |
| 83 | +// 添加初始单词到变换过程 |
| 84 | +addNewWord(result, currentWord.toString()); |
| 85 | + |
| 86 | +// 由于回溯是从后往前的,因此需要反转结果 |
| 87 | +// Collections.reverse(result); |
| 88 | +return result; |
| 89 | +} |
| 90 | + |
| 91 | +// 避免数据重复 |
| 92 | +private void addNewWord(List<String> list, String word) { |
| 93 | +if(list.size() > 0 && word.equals(list.get(list.size()-1))) { |
| 94 | +return; |
| 95 | +} |
| 96 | + |
| 97 | +list.add(word); |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +} |
0 commit comments