1662. 检查两个字符串数组是否相等

题目:给你两个字符串数组 word1 和 word2 。如果两个数组表示的字符串相同,返回 true ;否则,返回 false 。数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。 

示例 1:

输入:word1 = ["ab", "c"], word2 = ["a", "bc"]
输出:true
解释:
word1 表示的字符串为 "ab" + "c" -> "abc"
word2 表示的字符串为 "a" + "bc" -> "abc"
两个字符串相同,返回 true
示例 2:

输入:word1 = ["a", "cb"], word2 = ["ab", "c"]
输出:false
示例 3:

输入:word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
输出:true

1.原创

class Solution {
public:
    bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
        string res1,res2;
        for (auto i:word1)
            res1 += i;
        for (auto j:word2)
            res2 += j;
        if (res1==res2)
            return true;
        else
            return false;
    }
};

2.题解

class Solution {//不拼接字符串的方法
public:
    bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
        int p1 = 0, p2 = 0, i = 0, j = 0;
        while(p1 < word1.size() && p2 < word2.size()){
            if(word1[p1][i] != word2[p2][j]) 
                return false;
            i ++;
            if(i == word1[p1].size()) {
                p1++;
                i = 0;
            }
            j ++;
            if(j == word2[p2].size()) {
                p2++;
                j = 0;
            }
        }
        return p1 == word1.size() && p2 == word2.size();
    }
};

作者:liuyubobobo
链接:https://leetcode-cn.com/problems/check-if-two-string-arrays-are-equivalent/solution/bu-pin-jie-zi-fu-chuan-de-fang-fa-by-liuyubobobo/
posted @ 2021-03-11 10:40  USTC丶ZCC  阅读(179)  评论(0编辑  收藏  举报