前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【LeetCode击败99%+】检查两个字符串数组是否相等

【LeetCode击败99%+】检查两个字符串数组是否相等

作者头像
用户7886150
修改2021-04-29 14:42:25
8960
修改2021-04-29 14:42:25
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: Java程序来检查字符串是否是两个字符串的有效改组

题目 

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

提示: 

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

代码 

class Solution {

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

        StringBuilder w1 = new StringBuilder();

        StringBuilder w2 = new StringBuilder();

        for (int i = 0; i < word1.length; i++) {

            w1.append(word1[i]);

        }

        for (int i = 0; i < word2.length; i++) {

            w2.append(word2[i]);

        }

        return w1.toString().equals(w2.toString());

    }

}

结果 

用时内存击败100.00%击败55.78%

 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/check-if-two-string-arrays-are-equivalent/ 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档