1、检查两个字符串数组是否相等
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();
}
}
class Solution {
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
int p1 = 0, p2 = 0, i = 0, j = 0;
// 设置两个指针 \textit{p1}p1 和 \textit{p2}p2 分别表示遍历到了 \textit{word1}[\textit{p1}]word1[p1] 和 \textit{word2}[\textit{p2}]word2[p2],另外还需设置两个指针 ii 和 jj,表示正在对比 \textit{word1}[\textit{p1}][i]word1[p1][i] 和 \textit{word2}[\textit{p2}][j]word2[p2][j]。
while (p1 < word1.length && p2 < word2.length) {
if (word1[p1].charAt(i) != word2[p2].charAt(j)) {
return false;
}
i++;
if (i == word1[p1].length()) {
p1++;
i = 0;
}
j++;
if (j == word2[p2].length()) {
p2++;
j = 0;
}
}
return p1 == word1.length && p2 == word2.length;
}
}