给你两个字符串数组 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]
由小写字母组成
1. 利用指针,遍历,然后判断指针指向的字符是否相同,需要注意边界条件。
2. 利用字符串拼接,这个没啥好说的。
class Solution { public boolean arrayStringsAreEqual(String[] word1, String[] word2) { int i =0; int j = 0; int p1 = 0; int p2 = 0; while (i < word1.length || j < word2.length) { // 判断是否一边已经走完,而另一边还有剩余。 if (i >= word1.length || j >= word2.length) { return false; } if (word1[i].charAt(p1) != word2[j].charAt(p2)) { return false; } if (++ p1 == word1[i].length()) { p1 = 0; i ++; } if (++ p2 == word2[j].length()) { p2 = 0; j ++; } } return true; } }字符串拼接
class Solution { public boolean arrayStringsAreEqual(String[] word1, String[] word2) { return join(word1).equals(join(word2)); } public String join(String[] words) { StringBuilder ret = new StringBuilder(); for (String s : words) { ret.append(s); } return ret.toString(); } }
标签:力扣,return,String,---,word1,word2,字符串,true,1662 From: https://www.cnblogs.com/allWu/p/17219607.html