字符比较差异

package com.xiaoxun.emlsr.xml;

import org.apache.commons.lang.ObjectUtils;

public class Difference{ 
	
	public static void main(String[] args) throws Exception {
        //String str1 = "明天是否是啊 ";  
        //String str2 = "明他是否是你";  
		String str1 = "明是我否天";  
        String str2 = "明是你否";
        String highlightedDiff = highlightDifference(str2, str1);  
        System.out.println(highlightedDiff); 
        //明<span style='background-color: yellow;'>他</span>是否是<span style='background-color: yellow;'>你</span>


	}

    public static String highlightDifference(String str1, String str2) {  
        StringBuilder result = new StringBuilder();  
  
        int minLength = Math.min(str1.length(), str2.length());  
        int commonPrefixLength = getCommonPrefixLength(str1, str2);  
        int commonSuffixLength = getCommonSuffixLength(str1, str2);  
  
        result.append(str1.substring(0, commonPrefixLength));  
  
        for (int i = commonPrefixLength; i < minLength - commonSuffixLength; i++) {  
            if (i < str1.length() && str1.charAt(i) != str2.charAt(i)) {  
                result.append("<span style='background-color: yellow;'>");  
                result.append(Character.isWhitespace(str1.charAt(i))?"&nbsp;&nbsp;":str1.charAt(i));  
                result.append("</span>");  
            } else {  
                result.append(str1.charAt(i));  
            }  
        }  
        if(!org.apache.commons.lang3.ObjectUtils.isEmpty(str1.substring(minLength - commonSuffixLength))) {
        result.append("<span style='background-color: red;'>");  
        result.append(str1.substring(minLength - commonSuffixLength));
        result.append("</span>");  
        }
        return result.toString();  
    }  
  
    private static int getCommonPrefixLength(String str1, String str2) {  
        int minLength = Math.min(str1.length(), str2.length());  
        for (int i = 0; i < minLength; i++) {  
            if (str1.charAt(i) != str2.charAt(i)) {  
                return i;  
            }  
        }  
        return minLength;  
    }  
  
    private static int getCommonSuffixLength(String str1, String str2) {  
        int minLength = Math.min(str1.length(), str2.length());  
        for (int i = 0; i < minLength; i++) {  
            if (str1.charAt(str1.length() - 1 - i) != str2.charAt(str2.length() - 1 - i)) {  
                return i;  
            }  
        }  
        return minLength;  
    }  

}
这个案例更好些对比对的数据优化
```java
package com.xiaoxun.emlsr;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;

public class Difference {
public static void main(String[] args) {
    String str1 = "abcde";
    String str2 = "abxdfe"; 
    findLCS(str1, str2);
    diff001(str1, str2);
    
    System.out.println(lcs(str1, str2));//找出差异的字符   

}

static void diff001(String str1,String str2) {
	StringBuilder result1 = new StringBuilder();
    StringBuilder result2 = new StringBuilder();

    int i = 0, j = 0;
    while (i < str1.length() || j < str2.length()) {
        if (i < str1.length() && j < str2.length() && str1.charAt(i) == str2.charAt(j)) {
            result1.append(str1.charAt(i));
            result2.append(str2.charAt(j));
            i++;
            j++;
        } else {
            if (i < str1.length()) {
                result1.append("<span style='color:red'>").append(str1.charAt(i)).append("</span>");
                i++;
            }
            if (j < str2.length()) {
                result2.append("<span style='color:red'>").append(str2.charAt(j)).append("</span>");
                j++;
            }
        }
    }

    System.out.println(result1.toString());
    System.out.println(result2.toString());
}

private static String findLCS(String str1, String str2) {
    int[][] dp = new int[str1.length() + 1][str2.length() + 1];
    for (int i = 1; i <= str1.length(); i++) {
        for (int j = 1; j <= str2.length(); j++) {
            if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    StringBuilder lcsBuilder = new StringBuilder();
    int i = str1.length(), j = str2.length();
    while (i > 0 && j > 0) {
        if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
            lcsBuilder.append(str1.charAt(i - 1));
            i--;
            j--;
        } else if (dp[i - 1][j] > dp[i][j - 1]) {
            i--;
        } else {
            j--;
        }
    }

    return lcsBuilder.reverse().toString();
}



public static String lcs(String str1, String str2) {  
    int m = str1.length();  
    int n = str2.length();  
    int[][] dp = new int[m + 1][n + 1];  
    for (int i = 0; i <= m; i++) {  
        for (int j = 0; j <= n; j++) {  
            if (i == 0 || j == 0)  
                dp[i][j] = 0;  
            else if (str1.charAt(i - 1) == str2.charAt(j - 1))  
                dp[i][j] = dp[i - 1][j - 1] + 1;  
            else  
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);  
        }  
    }  
    return dp[m][n] == 0 ? "" : generateLCS(str1, str2, dp[m][n]);  
}  

private static String generateLCS(String str1, String str2, int len) {  
    StringBuilder sb = new StringBuilder();  
    int m = str1.length();  
    int n = str2.length();  
    int i = m - 1;  
    int j = n - 1;  
    while (len > 0) {  
        if (str1.charAt(i) == str2.charAt(j)) {  
            sb.insert(0, str1.charAt(i--));  
            len--;  
        } else if (str1.charAt(i) > str2.charAt(j)) {  
            i--;  
        } else {  
            j--;  
        }  
    }  
    return sb.toString();  
}  




public static String getLongestCommonSubsequence(String str1, String str2) {  
    int len1 = str1.length();  
    int len2 = str2.length();  
    int[][] dp = new int[len1 + 1][len2 + 1];  

    for (int i = 1; i <= len1; i++) {  
        for (int j = 1; j <= len2; j++) {  
            if (str1.charAt(i - 1) == str2.charAt(j - 1)) {  
                dp[i][j] = dp[i - 1][j - 1] + 1;  
            } else {  
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);  
            }  
        }  
    }  

    return str2.substring(0, dp[len1][len2]);  
}  

public static String markHtml(String str, String lcs) {  
    StringBuilder sb = new StringBuilder();  
    int len1 = str.length();  
    int len2 = lcs.length();  
    int minLen = Math.min(len1, len2);  
    int diffIndex = 0;  
    for (int i = 0; i < minLen; i++) {  
        char c1 = str.charAt(i);  
        char c2 = lcs.charAt(i);  
        if (c1 != c2) {  
            sb.append("<span style='color:red;'>").append(c1).append("</span>"); // 标记为红色  
            diffIndex = i + 1;  
        } else {  
            sb.append(c1); // 相同的字符不需要特殊标记  
        }  
    }  
    if (len1 > len2) {  
        for (int i = diffIndex; i < len1; i++) {  
            sb.append("<span style='color:red;'>").append(str.charAt(i)).append("</span>"); // 标记为红色  
        }  
    } else if (len2 > len1) {  
        for (int i = diffIndex; i < len2; i++) {  
            sb.append(lcs.charAt(i)); // 不需要特殊标记  
        }  
    } else {  
        // 如果两个字符串长度相同,则不需要额外处理  
    }  
    return sb.toString();  
}  


} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有知识的山巅

文章对你有用,学到了知识。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值