LeetCode (1662) - Check if two string arrays are equal

Article Directory

topic

1662. Check if two string arrays are equal.
Give you two string arrays word1 and word2. If the strings represented by the two arrays are the same, return true; otherwise, return false.
The string represented by the array is a string formed by concatenating all the elements in the array in order.

Example 1:

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

Example 2:

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

Example 3:

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

prompt:

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] 和 word2[i] 由小写字母组成

Problem solution (Java)

Used directly here return s1.equals(s2);, instead of the if-else statement

class Solution 
{
    
    
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) 
    {
    
    
        String s1 = "";
        String s2 = "";
        for(int index = 0;index < word1.length;index++)
        {
    
    
            s1 += word1[index];
        }
        for(int index = 0;index < word2.length;index++)
        {
    
    
            s2 += word2[index];
        }
        return s1.equals(s2);
    }
}

Guess you like

Origin blog.csdn.net/weixin_46841376/article/details/113956800