코린이의 소소한 공부노트

[LeetCode/Easy] 2068. Check Whether Two Strings are Almost Equivalent 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2068. Check Whether Two Strings are Almost Equivalent

무지맘 2023. 1. 12. 09:20

1. Input

1) 문자열 word1

2) 문자열 word2

 

2. Output

1) 두 문자열이 almost equivalent라면 true, 아니면 false

// Almost Equivalent의 조건

- 두 문자열을 이루고 있는 각 문자들의 빈도 수의 차가 3 이하여야 한다.

 

3. Constraint

1) n == word1.length == word2.length

2) 1 <= n <= 100

3) word1word2는 영어 소문자로만 이루어져 있다.

 

4. Example

Input: word1 = "aaaa", word2 = "bccb" -> Output: false

설명:

- bc의 빈도수 차는 각각 2이므로 3 이하라서 괜찮다.

- a의 경우 word1에서는 4번 나오는데, word2에서는 0번 나온다. -> 4 - 0 > 3 이므로 false

 

5. Code

1) 첫 코드(2022/07/05)

int[] n = new int[26];
for(int i=0 ; i<word1.length() ; i++){
    n[word1.charAt(i)-97]++;
    n[word2.charAt(i)-97]--;
}

for(int i=0 ; i<26 ; i++)
    if(Math.abs(n[i])>3)
        return false;

return true;