-
1790. Check if One String Swap Can Make Strings Equal javaLeetCode_Study_Plan/Programming Skills 2022. 9. 20. 16:09
Check if One String Swap Can Make Strings Equal - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
class Solution { public boolean areAlmostEqual(String s1, String s2) { String[] words1 = s1.split(""); String[] words2 = s2.split(""); int idx1 = -1, idx2 = -1; for (int i = 0; i < words1.length; i++) { System.out.println("words1: " + words1[i] + " words2: " + words2[i]); if (!words1[i].equals(words2[i])) { if (idx1 == -1) { idx1 = i; System.out.println("index: " + i + " idx1: " + idx1); } else if (idx2 == -1) { idx2 = i; System.out.println("index: " + i + " idx2: " + idx2); } else { return false; } } } if (idx1 < 0 && idx2 < 0) return true; if (idx2 < 0) return false; if (words1[idx1].equals(words2[idx2]) && words1[idx2].equals(words2[idx1])) return true; return false; } }
참고 : Java의 문자열 비교
https://kmj1107.tistory.com/207
[JAVA] 문자열(string) 비교 equals와 == 의 차이점 ( + equals의 반대)
equals와 == 은 어떤 차이점이 있을까요. 기본적으로 이 둘은 모두 양 쪽에 있는 내용을 비교한 값을 boolean type으로 반환한다는 공통점을 가집니다. 하지만 차이점이 분명 존재합니당. 1) 형태의
kmj1107.tistory.com
'==' vs 'equals' vs 'compare'
'LeetCode_Study_Plan > Programming Skills' 카테고리의 다른 글
496. Next Greater Element I java (0) 2022.09.20 589. N-ary Tree Preorder Traversal java (0) 2022.09.20 202. Happy Number java (0) 2022.09.20 1502. Can Make Arithmetic Progression From Sequence java (0) 2022.09.19 1822. Sign of the Product of an Array java (0) 2022.09.19