# 844.Backspace String Compare

\#844.Backspace String Compare 难度:Easy

> 给定 S 和 T 两个字符串，当它们分别被输入到空白的文本编辑器后，判断二者是否相等，并返回结果。 # 代表退格字符。

```
示例 1：

输入：S = "ab#c", T = "ad#c"
输出：true
解释：S 和 T 都会变成 “ac”。
示例 2：

输入：S = "ab##", T = "c#d#"
输出：true
解释：S 和 T 都会变成 “”。
示例 3：

输入：S = "a##c", T = "#a#c"
输出：true
解释：S 和 T 都会变成 “c”。
示例 4：

输入：S = "a#c", T = "b"
输出：false
解释：S 会变成 “c”，但 T 仍然是 “b”。
 

提示：

1 <= S.length <= 200
1 <= T.length <= 200
S 和 T 只含有小写字母以及字符 '#'。
```

需要求出去掉退格键之后的值，顺序不好求，可以倒序求，好在如果两个字符相等，则倒序之后也应该是一样。

```
class Solution {
public:
    bool backspaceCompare(string S, string T) {
        string s1="",t1="";
        int count=0;
        for(int i=S.length()-1;i>=0;--i)
            if(S[i]=='#')  ++count;
            else {
                if(count) 
                {
                    --count;
                }
            else
                s1+=S[i];
            }
        count=0;
        for(int i=T.length()-1;i>=0;i--)
            if(T[i]=='#') ++count;
            else
            {
                if(count)
                {
                    --count;
                }
                else
                t1+=T[i];
            }
 //      cout<< s1<<"+ " <<t1<<endl;
        return s1==t1;
    }
};
```

使用count统计退格键数量，如果不为零，则跳过当前字符。

> 执行用时 :0 ms, 在所有 C++ 提交中击败了100%的用户\
> 内存消耗 :8.4MB, 在所有 C++ 提交中击败了90.44%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/844.backspace_string_compare.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
