1662. Check if Two String Arrays Are Equivalent

1662. Check If Two String Arrays are Equivalent #

题目 #

  • 给定两个字符串数组word1word2。如果两个数组表示的字符串相同,返回true,否则返回false
  • 数组表示的字符串 是由数组中的所有元素 按顺序 连接形成的字符串。

思路 #

模拟 #

代码 #

模拟-双指针 #

class Solution {
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        int i = 0, p = 0;
        int j = 0, q = 0;
        while (i < word1.length && j < word2.length) {
            if (word1[i].charAt(p++) != word2[j].charAt(q++)) return false;
            if (p == word1[i].length()) { i++; p = 0; }
            if (q == word2[j].length()) { j++; q = 0; }
        }
        return i == word1.length && j == word2.length;
    }
}

模拟-API #

class Solution {
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        String ans1 = null, ans2 = null;
        for (String word: word1) ans1 += word;
        for (String word: word2) ans2 += word;
        return ans1.equals(ans2);
    }
}

致谢 #

宫水三叶