File tree Expand file tree Collapse file tree 2 files changed +84
-0
lines changed
algorithms/cpp/backspaceStringCompare Expand file tree Collapse file tree 2 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ LeetCode
1111| 859| [ Buddy Strings] ( https://leetcode.com/problems/buddy-strings/description/ ) | [ C++] ( ./algorithms/cpp/buddyStrings/BuddyStrings.cpp ) | Easy|
1212| 858| [ Mirror Reflection] ( https://leetcode.com/problems/mirror-reflection/description/ ) | [ C++] ( ./algorithms/cpp/mirrorReflection/MirrorReflection.cpp ) | Medium|
1313| 852| [ Peak Index in a Mountain Array] ( https://leetcode.com/problems/peak-index-in-a-mountain-array/description/ ) | [ C++] ( ./algorithms/cpp/peakIndexInAMountainArray/PeakIndexInAMountainArray.cpp ) | Easy|
14+ 844|[ Backspace String Compare] ( https://leetcode.com/problems/backspace-string-compare/description/ ) | [ C++] ( ./algorithms/cpp/backspaceStringCompare/BackspaceStringCompare.cpp ) |Easy|
1415| 837| [ Most Common Word] ( https://leetcode.com/problems/most-common-word/ ) | [ C++] ( ./algorithms/cpp/mostCommonWord/MostCommonWord.cpp ) | Easy|
1516| 804| [ Unique Morse Code Words] ( https://leetcode.com/problems/unique-morse-code-words/description/ ) | [ C++] ( ./algorithms/cpp/uniqueMorseCodeWords/UniqueMorseCodeWords.cpp ) | Easy|
1617| 771| [ Jewels and Stones] ( https://leetcode.com/problems/jewels-and-stones/description ) | [ C++] ( ./algorithms/cpp/jewelsAndStones/JewelsAndStones.cpp ) | Easy|
Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/backspace-string-compare/description/
2+ // Author : Hao Chen
3+ // Date : 2018-06-29
4+
5+ /* **************************************************************************************
6+ *
7+ * Given two strings S and T, return if they are equal when both are typed into empty
8+ * text editors. # means a backspace character.
9+ *
10+ *
11+ * Example 1:
12+ *
13+ *
14+ * Input: S = "ab#c", T = "ad#c"
15+ * Output: true
16+ * Explanation: Both S and T become "ac".
17+ *
18+ *
19+ *
20+ * Example 2:
21+ *
22+ *
23+ * Input: S = "ab##", T = "c#d#"
24+ * Output: true
25+ * Explanation: Both S and T become "".
26+ *
27+ *
28+ *
29+ * Example 3:
30+ *
31+ *
32+ * Input: S = "a##c", T = "#a#c"
33+ * Output: true
34+ * Explanation: Both S and T become "c".
35+ *
36+ *
37+ *
38+ * Example 4:
39+ *
40+ *
41+ * Input: S = "a#c", T = "b"
42+ * Output: false
43+ * Explanation: S becomes "c" while T becomes "b".
44+ *
45+ *
46+ * Note:
47+ *
48+ *
49+ * 1 <= S.length <= 200
50+ * 1 <= T.length <= 200
51+ * S and T only contain lowercase letters and '#' characters.
52+ *
53+ *
54+ * Follow up:
55+ *
56+ *
57+ * Can you solve it in O(N) time and O(1) space?
58+ *
59+ *
60+ *
61+ *
62+ ***************************************************************************************/
63+
64+ class Solution {
65+ private:
66+ void removeBackspaces (string &s) {
67+ int i = 0 ;
68+ for (int i=0 ; i<s.size (); i++) {
69+ if (s[i] == ' #' ) {
70+ int backSteps = i>0 ? 2 : 1 ;
71+ s.erase (i-backSteps + 1 , backSteps);
72+ i -= backSteps;
73+ }
74+ }
75+ }
76+
77+ public:
78+ bool backspaceCompare (string S, string T) {
79+ removeBackspaces (S);
80+ removeBackspaces (T);
81+ return S == T;
82+ }
83+ };
You can’t perform that action at this time.
0 commit comments