leetcode-1662-easy

发布时间 2023-12-06 08:50:51作者: iyiluo
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());
    }