Check If Two String Arrays are Equivalent
思路一:把第一个数组入队列,然后遍历比较第二个数组
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
Deque<Character> queue = new ArrayDeque<>();
for (String word : word1) {
for (char c : word.toCharArray()) {
queue.push(c);
}
}
for (String word : word2) {
for (char c : word.toCharArray()) {
if (queue.isEmpty() || queue.pollLast() != c) {
return false;
}
}
}
return queue.isEmpty();
}
思路二:变成字符串后比较
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
StringBuilder sb1 = new StringBuilder();
for (String word : word1) {
sb1.append(word);
}
StringBuilder sb2 = new StringBuilder();
for (String word : word2) {
sb2.append(word);
}
return sb1.toString().equals(sb2.toString());
}
标签:word,String,StringBuilder,1662,queue,word1,easy,word2,leetcode
From: https://www.cnblogs.com/iyiluo/p/17878734.html