首页 > 其他分享 >leetcode-1662-easy

leetcode-1662-easy

时间:2023-12-06 09:04:47浏览次数:31  
标签:word String StringBuilder 1662 queue word1 easy word2 leetcode

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

相关文章

  • leetcode-2169-easy
    CountOperationstoObtainZeroYouaregiventwonon-negativeintegersnum1andnum2.Inoneoperation,ifnum1>=num2,youmustsubtractnum2fromnum1,otherwisesubtractnum1fromnum2.Forexample,ifnum1=5andnum2=4,subtractnum2fromn......
  • leetcode-1646-easy
    GetMaximuminGeneratedArrayYouaregivenanintegern.A0-indexedintegerarraynumsoflengthn+1isgeneratedinthefollowingway:nums[0]=0nums[1]=1nums[2*i]=nums[i]when2<=2*i<=nnums[2*i+1]=nums[i]+nums[i+1]......
  • leetcode-1464-easy
    MaximumProductofTwoElementsinanArrayGiventhearrayofintegersnums,youwillchoosetwodifferentindicesiandjofthatarray.Returnthemaximumvalueof(nums[i]-1)*(nums[j]-1).Example1:Input:nums=[3,4,5,2]Output:12Explanation:I......
  • leetcode-1455-easy
    CheckIfaWordOccursAsaPrefixofAnyWordinaSentenceGivenasentencethatconsistsofsomewordsseparatedbyasinglespace,andasearchWord,checkifsearchWordisaprefixofanywordinsentence.Returntheindexofthewordinsentence(1-......
  • leetcode-2180-easy
    CountIntegersWithEvenDigitSumGivenapositiveintegernum,returnthenumberofpositiveintegerslessthanorequaltonumwhosedigitsumsareeven.Thedigitsumofapositiveintegeristhesumofallitsdigits.Example1:Input:num=4Ou......
  • leetcode-1572-easy
    MatrixDiagonalSumGivenasquarematrixmat,returnthesumofthematrixdiagonals.Onlyincludethesumofalltheelementsontheprimarydiagonalandalltheelementsonthesecondarydiagonalthatarenotpartoftheprimarydiagonal.Example1:......
  • leetcode-1550-easy
    ThreeConsecutiveOddsGivenanintegerarrayarr,returntrueiftherearethreeconsecutiveoddnumbersinthearray.Otherwise,returnfalse.Example1:Input:arr=[2,6,4,1]Output:falseExplanation:Therearenothreeconsecutiveodds.Example2:......
  • leetcode-1512-easy
    NumberofGoodPairsGivenanarrayofintegersnums,returnthenumberofgoodpairs.Apair(i,j)iscalledgoodifnums[i]==nums[j]andi<j.Example1:Input:nums=[1,2,3,1,1,3]Output:4Explanation:Thereare4goodpairs(0,3),(0,4),(......
  • CF1824B1 LuoTianyi and the Floating Islands (Easy Version) 题解
    题意:思路:由于$k∈[1,3]$,分类讨论:当$k=1$时,有人结点自身即为好结点,每种情况的期望为$\frac{1}{n}$,$n$种情况的期望和为$1$。最终答案即为$1$。当$k=2$时,$2$个有人结点之间的路径上的结点即为好结点,那么问题转化为:树上所有路径的结点......
  • C1. Good Subarrays (Easy Version)
    思路:我们枚举每一个左端点,对于每一个左端点,寻找最长的满足条件的区间,这个区间长度就是左端点对答案的贡献,可以发现具有单调性,右端点只会前进不会倒退。所以我们两个指针各扫一遍区间就可以。#include<bits/stdc++.h>#definelsp<<1#definersp<<1|1#definePIIpair<int,......