小白 LeetCode 5605 检查两个字符串数据是否相等

题目给你两个字符串数组 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

思路:先把字符串数组合并成字符串,再比较两个字符串是否相等,若相等,则返回true;否则,返回false。

class Solution {
    
    
    public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
    
    
        
        String s1 = "";
        String s2 = "";
        for(int i = 0; i < word1.length; i++){
    
    
            s1 = s1.concat(word1[i]);
        }
        for(int j = 0; j < word2.length; j++){
    
    
            s2 = s2.concat(word2[j]);
        }
        if(s1.equals(s2))
            return true;
        else
            return false;
    }
}

在这里插入图片描述

  • concat() 方法:用于将指定的字符串参数连接到字符串上。

猜你喜欢

转载自blog.csdn.net/tsundere_x/article/details/109957867