1 package solution39; 2 3 class Solution { 4 public int solution(String s1,String s2) { 5 int n1 = s1.length(); 6 int n2 = s2.length(); 7 int count = 0; 8 int i = n1-1; 9 int j = n2-1; 10 while(i>=0 && j>=0){ 11 if(s1.charAt(i)==s2.charAt(j)){ 12 count += 1; 13 i -= 1; 14 j -= 1; 15 } 16 else{ 17 break; 18 } 19 } 20 return n1 + n2 - 2 * count; 21 } 22 }
算法思路:贪心算法。
两个字符串从后向前逐字符比对,如果发现不同字符,则停止比对。
来源:https://www.cnblogs.com/asenyang/p/12425127.html