본문 바로가기
알고리즘 풀이

231201 LeetCode 문제 풀이

by 미노킴 2023. 12. 1.

1662. Check If Two String Arrays are Equivalent

https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/description/?envType=daily-question&envId=2023-12-01

 

1) 문제 설명

Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.

A string is represented by an array if the array elements concatenated in order forms the string.

 

Example 1:

Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.

Example 2:

Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
Output: false

Example 3:

Input: word1  = ["abc", "d", "defg"], word2 = ["abcddefg"]
Output: true

 

2) 제한 사항

  • 1 <= word1.length, word2.length <= 103
  • 1 <= word1[i].length, word2[i].length <= 103
  • 1 <= sum(word1[i].length), sum(word2[i].length) <= 103
  • word1[i] and word2[i] consist of lowercase letters.

 

3) 도전 과제

X

 

4) 풀이

String, StringBuilder를 이용해 각각 풀어보았는데, StringBulider 쪽이 더 빨랐다.

 

양쪽 다 걸리는 시간이 길지 않고, 워낙 간단한 문제여서 비교하기가 애매하다.

 

여기선 가볍게 넘어가자.

 

5) 소스 코드 및 결과

class Solution {
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        StringBuilder result1 = new StringBuilder();
        StringBuilder result2 = new StringBuilder();

        for(String s : word1){
            result1.append(s);
        }

        for(String s : word2){
            result2.append(s);
        }

        String newWord1 = result1.toString();
        String newWord2 = result2.toString();
        
        return newWord1.equals(newWord2);
    }
}

 

6) 다른 사람의 풀이

X

'알고리즘 풀이' 카테고리의 다른 글

240306 LeetCode 문제 풀이  (0) 2024.03.06
231209 LeetCode 문제 풀이  (0) 2023.12.09
231115 LeetCode 문제 풀이  (0) 2023.11.15
23114 LeetCode 문제 풀이  (0) 2023.11.14
231113 LeetCode 문제 풀이  (2) 2023.11.13