Skip to content

Commit 0e8a39d

Browse files
committed
Code + Output P24 2 approaches
1 parent 4a61039 commit 0e8a39d

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

src/Problem-24/code-2pointer.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* **************************************************************************
2+
* Copyright 2024 The OpenGenus.org Authors. All Rights Reserved.
3+
*
4+
* Code for the book "DAILY 43: Algorithmic Problems for Coding Interviews: Easy level, C++ edition"
5+
*
6+
* Licensed under the GNU General Public License, Version 3.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.gnu.org/licenses/gpl-3.0.html
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* For details about the book, please visit: https://www.amazon.com/dp/B0CZJNBLQS
19+
* *************************************************************************/
20+
21+
#include <iostream>
22+
#include <string>
23+
24+
class Solution {
25+
public:
26+
bool backspaceCompare(std::string S, std::string T) {
27+
int i = S.size() - 1, j = T.size() - 1, countA = 0, countB = 0;
28+
while (i >= 0 || j >= 0) {
29+
while (i >= 0 && (S[i] == '#' || countA > 0))
30+
S[i--] == '#' ? ++countA : --countA;
31+
while (j >= 0 && (T[j] == '#' || countB > 0))
32+
T[j--] == '#' ? ++countB : --countB;
33+
if (i < 0 || j < 0)
34+
return i == j;
35+
if (S[i--] != T[j--])
36+
return false;
37+
}
38+
return i == j;
39+
}
40+
};
41+
42+
int main() {
43+
Solution sol;
44+
std::string S1 = "ab#c", T1 = "ad#c"; // true
45+
std::string S2 = "ab##", T2 = "c#d#"; // true
46+
std::string S3 = "a##c", T3 = "#a#c"; // true
47+
std::string S4 = "a#c", T4 = "b"; // false
48+
49+
std::cout << "S: " << S1 << ", T: " << T1 << " -> " << (sol.backspaceCompare(S1, T1) ? "true" : "false") << std::endl;
50+
std::cout << "S: " << S2 << ", T: " << T2 << " -> " << (sol.backspaceCompare(S2, T2) ? "true" : "false") << std::endl;
51+
std::cout << "S: " << S3 << ", T: " << T3 << " -> " << (sol.backspaceCompare(S3, T3) ? "true" : "false") << std::endl;
52+
std::cout << "S: " << S4 << ", T: " << T4 << " -> " << (sol.backspaceCompare(S4, T4) ? "true" : "false") << std::endl;
53+
54+
return 0;
55+
}

src/Problem-24/code-stack.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* **************************************************************************
2+
* Copyright 2024 The OpenGenus.org Authors. All Rights Reserved.
3+
*
4+
* Code for the book "DAILY 43: Algorithmic Problems for Coding Interviews: Easy level, C++ edition"
5+
*
6+
* Licensed under the GNU General Public License, Version 3.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.gnu.org/licenses/gpl-3.0.html
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* For details about the book, please visit: https://www.amazon.com/dp/B0CZJNBLQS
19+
* *************************************************************************/
20+
21+
#include <iostream>
22+
#include <stack>
23+
#include <string>
24+
25+
class Solution {
26+
public:
27+
bool backspaceCompare(std::string S, std::string T) {
28+
std::stack<int> s1, s2;
29+
std::string str1, str2;
30+
for (int i = 0; i < S.size(); i++) {
31+
if (S[i] == '#' && !s1.empty())
32+
s1.pop();
33+
else if (S[i] != '#')
34+
s1.push(S[i]);
35+
}
36+
for (int i = 0; i < T.size(); i++) {
37+
if (T[i] == '#' && !s2.empty())
38+
s2.pop();
39+
else if (T[i] != '#')
40+
s2.push(T[i]);
41+
}
42+
while (!s1.empty()) {
43+
str1.push_back(s1.top());
44+
s1.pop();
45+
}
46+
while (!s2.empty()) {
47+
str2.push_back(s2.top());
48+
s2.pop();
49+
}
50+
return str1 == str2;
51+
}
52+
};
53+
54+
int main() {
55+
Solution sol;
56+
std::string S1 = "ab#c", T1 = "ad#c"; // true
57+
std::string S2 = "ab##", T2 = "c#d#"; // true
58+
std::string S3 = "a##c", T3 = "#a#c"; // true
59+
std::string S4 = "a#c", T4 = "b"; // false
60+
61+
std::cout << "S: " << S1 << ", T: " << T1 << " -> " << (sol.backspaceCompare(S1, T1) ? "true" : "false") << std::endl;
62+
std::cout << "S: " << S2 << ", T: " << T2 << " -> " << (sol.backspaceCompare(S2, T2) ? "true" : "false") << std::endl;
63+
std::cout << "S: " << S3 << ", T: " << T3 << " -> " << (sol.backspaceCompare(S3, T3) ? "true" : "false") << std::endl;
64+
std::cout << "S: " << S4 << ", T: " << T4 << " -> " << (sol.backspaceCompare(S4, T4) ? "true" : "false") << std::endl;
65+
66+
return 0;
67+
}

src/Problem-24/output-2pointer.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
S: ab#c, T: ad#c -> true
2+
S: ab##, T: c#d# -> true
3+
S: a##c, T: #a#c -> true
4+
S: a#c, T: b -> false

src/Problem-24/output-stack.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
S: ab#c, T: ad#c -> true
2+
S: ab##, T: c#d# -> true
3+
S: a##c, T: #a#c -> true
4+
S: a#c, T: b -> false

0 commit comments

Comments
 (0)