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

leetcode-844-easy

时间:2023-04-14 22:37:33浏览次数:41  
标签:count 844 String else char easy chars2 chars1 leetcode

Backspace String Compare

Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

Example 1:

Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
Example 2:

Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
Example 3:

Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".
Constraints:

1 <= s.length, t.length <= 200
s and t only contain lowercase letters and '#' characters.
Follow up: Can you solve it in O(n) time and O(1) space?

思路一:用栈实现

    public boolean backspaceCompare(String s, String t) {
        char[] chars1 = s.toCharArray();
        char[] chars2 = t.toCharArray();

        Stack<Character> stack1 = new Stack<>();
        for (char c : chars1) {
            if (c == '#' && !stack1.isEmpty()) {
                stack1.pop();
            } else if (c != '#') {
                stack1.push(c);
            }
        }

        Stack<Character> stack2 = new Stack<>();
        for (char c : chars2) {
            if (c == '#' && !stack2.isEmpty()) {
                stack2.pop();
            } else if (c != '#') {
                stack2.push(c);
            }
        }

        return stack1.equals(stack2);
    }

思路二: 从字符串后面往前面遍历,可以省去栈的调用

    public boolean backspaceCompare2(String s, String t) {
    char[] chars1 = s.toCharArray();
    char[] chars2 = t.toCharArray();

        StringBuilder s1 = new StringBuilder();
        int count = 0;
        for (int i = chars1.length - 1; i >= 0; i--) {
            char c = chars1[i];
            if (c == '#') {
                count++;
            } else {
                if (count == 0) {
                    s1.append(c);
                } else {
                    count--;
                }
            }
        }

        StringBuilder s2 = new StringBuilder();
        count = 0;
        for (int i = chars2.length - 1; i >= 0; i--) {
            char c = chars2[i];
            if (c == '#') {
                count++;
            } else {
                if (count == 0) {
                    s2.append(c);
                } else {
                    count--;
                }
            }
        }

        return s1.toString().equals(s2.toString());
    }

标签:count,844,String,else,char,easy,chars2,chars1,leetcode
From: https://www.cnblogs.com/iyiluo/p/17320134.html

相关文章

  • leetcode-830-easy
    PositionsofLargeGroupsInastringsoflowercaseletters,theselettersformconsecutivegroupsofthesamecharacter.Forexample,astringlikes="abbxxxxzyy"hasthegroups"a","bb","xxxx","z"......
  • leetcode-812-easy
    LargestTriangleAreaGivenanarrayofpointsontheX-Yplanepointswherepoints[i]=[xi,yi],returntheareaofthelargesttrianglethatcanbeformedbyanythreedifferentpoints.Answerswithin10-5oftheactualanswerwillbeaccepted.Exampl......
  • leetcode-944-easy
    DeleteColumnsToMakeSortedYouaregivenanarrayofnstringsstrs,allofthesamelength.Thestringscanbearrangedsuchthatthereisoneoneachline,makingagrid.Forexample,strs=["abc","bce","cae"]canb......
  • 代码随想录算法训练营Day01 | LeetCode704 二分查找、Leetcode27 移除元素
    今日学习的视频和文章代码随想录数组基础复习基础知识代码随想录二分查找代码随想录移除元素LeetCode704二分查找题目链接:704.二分查找-力扣(Leetcode)以前学二分查找的时候,真的一直搞不清楚怎么操作左边界和有边界,以及循环的终止条件是什么,总是自己慢慢调试出来,......
  • LeetCode 周赛 340,质数 / 前缀和 / 极大化最小值 / 最短路 / 平衡二叉树
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。上周跟大家讲到小彭文章风格的问题,和一些朋友聊过以后,至少在算法题解方面确定了小彭的风格。虽然竞赛算法题的文章受众非常小,但却有很多像我一样的初学者,他们有兴趣参加但容易被题目难......
  • 视频直播点播EasyDSS迁移至新服务器,启动正常但无法访问是什么原因?
    EasyDSS能实现视频流媒体的上传、转码、存储、录像、推拉流、直播、点播等功能,具备超低延迟、超高画质、超大并发访问量等特点,可应用在多样化的场景中,如:在线课堂、教育直播、校园活动直播、企业培训、游戏直播等。平台支持HTTP、HLS、RTMP等播出协议,并且兼容多终端,如:Windows、Andro......
  • 在EasyCVR中点击电子地图,出现快照不消失情况是什么原因?
    EasyCVR视频融合平台基于云边端一体化架构,部署轻快、功能灵活,平台可支持多协议、多类型设备接入,包括:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、大华SDK、宇视SDK等(具体见下图)。在视频能力上,可实现视频直播、录像、回放、检索、云存储、告警上报、语音对讲、电子地图、集......
  • LeetCode 108.将有序数组转换成二叉搜索树
    1.题目:给你一个整数数组nums,其中元素已经按升序排列,请你将其转换为一棵高度平衡二叉搜索树。高度平衡二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过1」的二叉树。示例1:输入:nums=[-10,-3,0,5,9]输出:[0,-3,9,-10,null,5]解释:[0,-10,5,null,-3,null,......
  • [LeetCode] 1440. Jump Game V 跳跃游戏之五
    Givenanarrayof integers arr andaninteger d.Inonestepyoucanjumpfromindex i toindex:i+x where: i+x<arr.length and 0< x<=d.i-x where: i-x>=0 and 0< x<=d.Inaddition,youcanonlyjumpfromindex i toi......
  • 【前缀和】LeetCode 1031. 两个非重叠子数组的最大和
    题目链接1031.两个非重叠子数组的最大和思路代码classSolution{publicintmaxSumTwoNoOverlap(int[]nums,intfirstLen,intsecondLen){//求一个前缀和for(inti=1;i<nums.length;++i){nums[i]+=nums[i-1];}......