DEV Community

Cover image for Leet Code — 1662. Check If Two String Arrays are Equivalent
Ben Pereira
Ben Pereira

Posted on

Leet Code — 1662. Check If Two String Arrays are Equivalent

It’s an easy problem on leet code, description being:

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

Constraints:

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.

At first glance one thing you could do is to join all the strings as one and then check using equals. I did that using Stream as you can see down bellow:

class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
    return Stream.of(word1).collect(Collectors.joining(""))
            .equals(Stream.of(word2).collect(Collectors.joining("")));
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 3 ms, faster than 6.77% of Java online submissions for Check If Two String Arrays are Equivalent.

Memory Usage: 40.2 MB, less than 87.17% of Java online submissions for Check If Two String Arrays are Equivalent.

Simple and straight, but if you can’t generate new instances or primitives then you would have to iterate through the arrays and compare the results:

public boolean arrayStringsAreEqual(String[] word1, String[] word2) {

        int w1Size = word1.length, w1ArrIndex = 0, w1StrIndex = 0;
        int w2Size = word2.length, w2ArrIndex = 0, w2StrIndex = 0;

        while(w1ArrIndex != w1Size && w2ArrIndex != w2Size) {
            if(word1[w1ArrIndex].charAt(w1StrIndex) != word2[w2ArrIndex].charAt(w2StrIndex)) {
                return false;
            }

            w1StrIndex++;
            w2StrIndex++;

            if(word1[w1ArrIndex].length() == w1StrIndex) {
                w1StrIndex=0;
                w1ArrIndex++;
            }

            if(word2[w2ArrIndex].length() == w2StrIndex) {
                w2StrIndex=0;
                w2ArrIndex++;
            }
        }

        return w1ArrIndex == w1Size && w2ArrIndex == w2Size;
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 3 ms, faster than 6.87% of Java online submissions for Check If Two String Arrays are Equivalent.

Memory Usage: 39.9 MB, less than 99.01% of Java online submissions for Check If Two String Arrays are Equivalent.

On return I’ve made sure that I’ve checked the indexes so I could avoid any potential input that could’ve missed a character.

One interesting thing that I was thinking was the idea of using an iterator to do this, would make much simpler, then scrolling through discussions on leet code I found one result where the person did one by itself and used on the solution, very clever!

class CharIterator { 
    String[] words;
    int wordIndex = 0;
    int charIndex = 0;

    CharIterator(String[] words) { 
        this.words = words;
    } 

    public boolean hasNext() { 
        return this.wordIndex != this.words.length;
    } 

    public char next() {
        char currChar = words[wordIndex].charAt(charIndex++);
        if (charIndex == words[wordIndex].length()) {
            charIndex = 0;
            wordIndex++;
        }
        return currChar;
    }
}

class Solution {
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
        CharIterator charIterator1 = new CharIterator(word1);
        CharIterator charIterator2 = new CharIterator(word2);
        while (charIterator1.hasNext() && charIterator2.hasNext()) {
            if (charIterator1.next() != charIterator2.next()) {
                return false;
            }
        }
        return !charIterator1.hasNext() && !charIterator2.hasNext();
    }
}
Enter fullscreen mode Exit fullscreen mode

kudos for o_inari, and you can check the discussion here.


That’s it! If there is anything thing else to discuss feel free to drop a comment, until next post! :)

Top comments (0)