반응형

https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/description

 

Check If Two String Arrays are Equivalent - LeetCode

Can you solve this real interview question? Check If Two String Arrays are Equivalent - 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

leetcode.com

 

word1과 word2의 문자를 하나씩 탐색하며 비교해줍니다.

class Solution {
public:
    bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
        int i=0, ii=0, j=0, jj=0;
        while(i < word1.size() && j < word2.size()) {
            if(word1[i][ii] != word2[j][jj]) {
                return false;
            }

            ii++, jj++;

            if(ii == word1[i].size()) {
                ii = 0;
                i++;
            }
            if(jj == word2[j].size()) {
                jj = 0;
                j++;
            }
        }

        return i == word1.size() && j == word2.size();
    }
};
반응형

+ Recent posts