前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >844. Backspace String Compare

844. Backspace String Compare

作者头像
用户1631856
发布2018-09-27 17:00:45
7330
发布2018-09-27 17:00:45
举报
文章被收录于专栏:老秦求学老秦求学

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

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 = "#a#c" Output: true Explanation: Both S and T become "c".

Example 4:

Input: S = "a#c", T = "b" Output: false Explanation: S becomes "c" while T becomes "b". Note:

1 <= S.length <= 200 1 <= T.length <= 200 S and T only contain lowercase letters and '#' characters.

class Solution {
public:
    bool backspaceCompare(string S, string T) {
        string s = "", t="";
        for(int i=0;i<S.length();i++){
            if(S[i] == '#'){//注意是字符之间的比较,不能用字符和字符串进行比较-->错误ISO C forbids comparison between pointer and integer [-fpermissive]
                if(s.length()!=0)
                    s.pop_back();
            }
            else{
                s+=S[i];
            }
        }
        for(int i=0;i<T.length();i++){
            if(T[i] == '#'){
                if(t.length()!=0)
                    t.pop_back();
            }
            else{
                t+=T[i];
            }
        }
        return s==t;
    }
};
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档