leetcode 1662. Check If Two String Arrays are Equivalent(判断两个字符串数组是否相等)

博客围绕判断两个字符串数组拼接后是否相等展开。给出示例,如输入[“ab”, “c”]和[“a”, “bc”],拼接后均为“abc”,结果为true;输入[“a”, “cb”]和[“ab”, “c”],拼接后不同,结果为false。思路是用StringBuilder拼接后判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

把两个字符串数组中的字符串拼接,然后判断它们是否相等。

思路:

用StringBuilder拼接,然后判断是否相等即可。

public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
    StringBuilder str1 = new StringBuilder();
    StringBuilder str2 = new StringBuilder();
    int n = Math.max(word1.length, word2.length);
    
    for(int i = 0; i < n; i++) {
        if(i < word1.length) str1.append(word1[i]);
        if(i < word2.length) str2.append(word2[i]);
    }
    
    return str1.toString().equals(str2.toString());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值