Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Method
- java
- Tree
- Class
- Number Theory
- 자바
- greedy
- implement
- hash table
- simulation
- 구현
- Binary Search
- bit manipulation
- geometry
- Matrix
- string
- 파이썬
- dynamic programming
- two pointers
- array
- sorting
- 코딩테스트
- 코테
- Counting
- database
- Binary Tree
- Math
- SQL
- Data Structure
- Stack
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1790. Check if One String Swap Can Make Strings Equal 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1790. Check if One String Swap Can Make Strings Equal
무지맘 2023. 4. 21. 10:181. Input
1) String s1
2) String s2
2. Output
1) s1에 있는 문자 2개의 위치를 최대 1번 바꿔서 s2와 같아진다면 true를, 아니면 false를 반환
3. Constraint
1) 1 <= s1.length, s2.length <= 100
2) s1.length == s2.length
3) s1과 s2는 영어 소문자로만 이루어져 있다.
4. Example
Input: s1 = "bank", s2 = "kanb" -> Output: true
Input: s1 = "attack", s2 = "defend" -> Output: false
Input: s1 = "kelb", s2 = "kelb" -> Output: true
5. Code
1) 첫 코드(2023/04/21)
ArrayList<Integer> index = new ArrayList<>();
for(int i=0 ; i<s1.length() ; i++)
if(s1.charAt(i)!=s2.charAt(i))
index.add(i);
boolean answer;
if(index.size()==0)
answer = true;
else if(index.size()!=2)
answer = false;
else
answer = s1.charAt(index.get(0))==s2.charAt(index.get(1))
&& s1.charAt(index.get(1))==s2.charAt(index.get(0));
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1800. Maximum Ascending Subarray Sum (0) | 2023.04.21 |
---|---|
[LeetCode/Easy] 1796. Second Largest Digit in a String (0) | 2023.04.21 |
[백준 온라인 저지] 24060. 알고리즘 수업 - 병합 정렬 1 (0) | 2023.04.20 |
[백준 온라인 저지] 25501. 재귀의 귀재 (0) | 2023.04.20 |
[백준 온라인 저지] 10870. 피보나치 수 5 (0) | 2023.04.20 |