一、题目描述
二、题解
方法一:
import java.util.*;
public class Main{ public static void main(String[] args) { Scanner sc &#61; new Scanner(System.in);String s1 &#61; sc.next();String s2 &#61; sc.next();if (s1.length() !&#61; s2.length()) {System.out.println(1);return;}if (s1.equals(s2)) {System.out.println(2);return;}char[] S1 &#61; s1.toCharArray();char[] S2 &#61; s2.toCharArray();int cnt &#61; 0;for (int i &#61; 0; i < s1.length(); i&#43;&#43;) {if (S1[i] &#61;&#61; S2[i] || S1[i] &#43; 32 &#61;&#61; S2[i] || S1[i] - 32 &#61;&#61; S2[i]) {cnt&#43;&#43;;}}if (cnt &#61;&#61; S1.length) {System.out.println(3); return; }else System.out.println(4);}
}
复杂度分析
- 时间复杂度&#xff1a;O(n)O(n)O(n)&#xff0c;
- 空间复杂度&#xff1a;O(1)O(1)O(1)&#xff0c;
方法二&#xff1a;库函数
- 在验证提条件3时&#xff0c;直接使用以下语句简化代码&#xff1a;
s1.toLowerCase().equals(s2.toLowerCase()) && !s1.equals(s2)