-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path844-Backspace-String-Compare.cpp
More file actions
64 lines (60 loc) · 1.81 KB
/
844-Backspace-String-Compare.cpp
File metadata and controls
64 lines (60 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Author : Vicen-te
// Date : 10/19/2023 (MM-DD-YYYY)
/**
*
* Description:
* 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.
*
* Ex1.
* Input: s = "ab#c", t = "ad#c"
* Output: true
* Explanation: Both s and t become "ac".
*
* Ex2.
* Input: s = "a#c", t = "b"
* Output: true
* Explanation: Both s and t become "".
*
* Ex3.
* Input: s = "ab#c", t = "ad#c"
* Output: false
* Explanation: s becomes "c" while t becomes "b".
*
* Algorithm:
* 1. Search for the first occurrence of '#' in the string and position the iterator 'it'
* at that character.
* 2. Continuously remove the '#' character and the preceding character (if applicable)
* pointed to by 'it' until there are no more '#' characters in the string.
* 3. After all backspaces have been removed, compare the modified strings 's' and 't'.
* 4. If 's' and 't' are identical, return true; otherwise, return false.
*
* Time: O(N)
* Space: O(1)
*/
class Solution {
public:
bool backspaceCompare(string s, string t) {
removeLetters(s);
removeLetters(t);
return t == s;
}
void removeLetters(string& s)
{
string::iterator it = find(s.begin(), s.end(), '#');
while(it != s.end())
{
if(it == s.begin())
{
s.erase(it);
it = find(s.begin(), s.end(), '#');
continue;
}
s.erase(it);
s.erase(it-1);
it = find(s.begin(), s.end(), '#');
}
}
};