Check If Two String Arrays are Equivalent

easy string two pointers

Problem

Given two arrays of strings word1 and word2, return whether concatenating each gives the same string.

Inputword1 = ["ab","c"], word2 = ["a","bc"]
Outputtrue
"ab"+"c" = "abc" = "a"+"bc".

def array_strings_are_equal(word1, word2):
    i1 = j1 = i2 = j2 = 0
    while i1 < len(word1) and i2 < len(word2):
        if word1[i1][j1] != word2[i2][j2]:
            return False
        j1 += 1
        if j1 == len(word1[i1]): i1 += 1; j1 = 0
        j2 += 1
        if j2 == len(word2[i2]): i2 += 1; j2 = 0
    return i1 == len(word1) and i2 == len(word2)
function arrayStringsAreEqual(word1, word2) {
  let i1 = 0, j1 = 0, i2 = 0, j2 = 0;
  while (i1 < word1.length && i2 < word2.length) {
    if (word1[i1][j1] !== word2[i2][j2]) return false;
    if (++j1 === word1[i1].length) { i1++; j1 = 0; }
    if (++j2 === word2[i2].length) { i2++; j2 = 0; }
  }
  return i1 === word1.length && i2 === word2.length;
}
class Solution {
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        int i1 = 0, j1 = 0, i2 = 0, j2 = 0;
        while (i1 < word1.length && i2 < word2.length) {
            if (word1[i1].charAt(j1) != word2[i2].charAt(j2)) return false;
            if (++j1 == word1[i1].length()) { i1++; j1 = 0; }
            if (++j2 == word2[i2].length()) { i2++; j2 = 0; }
        }
        return i1 == word1.length && i2 == word2.length;
    }
}
bool arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {
    int i1 = 0, j1 = 0, i2 = 0, j2 = 0;
    while (i1 < (int)word1.size() && i2 < (int)word2.size()) {
        if (word1[i1][j1] != word2[i2][j2]) return false;
        if (++j1 == (int)word1[i1].size()) { i1++; j1 = 0; }
        if (++j2 == (int)word2[i2].size()) { i2++; j2 = 0; }
    }
    return i1 == (int)word1.size() && i2 == (int)word2.size();
}
Time: O(n) Space: O(1)