Skip to content

Commit 244ee88

Browse files
authored
Merge pull request #789 from aaradhyasinghgaur/adding-lc-solution2
Adding LC solution for 1544. Make The String Great
2 parents 5d21a5d + 43fb210 commit 244ee88

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
id: make-the-string-great
3+
title: Make The String Great (Leetcode)
4+
sidebar_label: 1544-MakeTheStringGreat
5+
description: Given a string s of lower and upper case English letters.
6+
---
7+
8+
## Problem Description
9+
10+
| Problem Statement | Solution Link | LeetCode Profile |
11+
| :---------------- | :------------ | :--------------- |
12+
| [Make The String Great](https://leetcode.com/problems/make-the-string-great/description/) | [Make The String Great Solution on LeetCode](https://leetcode.com/problems/make-the-string-great/solutions) | [Aaradhya Singh ](https://leetcode.com/u/keira_09/) |
13+
14+
15+
## Problem Description
16+
17+
Given a string $s$ of lower and upper case English letters.
18+
19+
A good string is a string which doesn't have two adjacent characters $s[i]$ and $s[i + 1]$ where:
20+
21+
- $0 <= i <= s.length - 2$
22+
- s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.
23+
24+
To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.
25+
26+
Return the string after making it good. The answer is guaranteed to be unique under the given constraints.
27+
28+
Notice that an empty string is also good.
29+
30+
### Examples
31+
32+
#### Example 1
33+
34+
- **Input:** s = "leEeetcode"
35+
- **Output:** "leetcode"
36+
- **Explanation:** In the first step, either you choose $i = 1$ or $i = 2$, both will result $"leEeetcode"$ to be reduced to $"leetcode"$.
37+
38+
39+
#### Example 2
40+
41+
- **Input:** s = "abBAcC"
42+
- **Output:** ""
43+
- **Explanation:** We have many possible scenarios, and all lead to the same answer. For example: <br />
44+
"abBAcC" --> "aAcC" --> "cC" --> ""
45+
"abBAcC" --> "abBA" --> "aA" --> ""
46+
47+
#### Example 2
48+
49+
- **Input:** s = "s"
50+
- **Output:** "s"
51+
52+
53+
### Constraints
54+
55+
56+
- $1 <= s.length <= 100$
57+
- s contains only lower and upper case English letters.
58+
59+
60+
61+
### Intuition
62+
63+
The code aims to remove adjacent pairs of characters in the input string s where one character is the uppercase version of the other (e.g., 'a' and 'A'). It initializes an empty string result and iterates through s. If result is empty, it adds the current character. Otherwise, it checks if the current character and the last character of result form such a pair. If they do, it removes the last character from result; otherwise, it appends the current character to result. This approach efficiently creates a "good" string with no adjacent pairs of uppercase-lowercase characters, providing the desired output.
64+
65+
### Approach
66+
67+
1. **Initialize an Empty String:**
68+
69+
- Initialize an empty string result to store the characters after processing.
70+
71+
2. **Iterate Through the Input String:**
72+
73+
- Iterate through each character in the input string s using a for loop.
74+
75+
3. **Check for Empty String:**
76+
77+
- If result is empty, add the current character to result.
78+
79+
4. **Check for Adjacent Pairs:**
80+
81+
- If result is not empty, check if the current character and the last character of result form an adjacent pair where one character is the uppercase version of the other (e.g., 'a' and 'A').
82+
- If such a pair is found, remove the last character from result using result.erase(result.size() - 1).
83+
- If no adjacent pair is found, append the current character to result.
84+
85+
5. **Return the Resulting String:**
86+
87+
- After processing all characters in s, return the final value of result, which represents the "good" string without adjacent pairs of uppercase-lowercase characters.
88+
89+
### Solution Code
90+
91+
#### Python
92+
93+
```py
94+
class Solution:
95+
def makeGood(self, s: str) -> str:
96+
result = []
97+
98+
for char in s:
99+
if not result:
100+
result.append(char)
101+
else:
102+
if result[-1].lower() == char.lower() and (result[-1] != char):
103+
result.pop()
104+
else:
105+
result.append(char)
106+
107+
return ''.join(result)
108+
```
109+
110+
#### Java
111+
112+
```java
113+
class Solution {
114+
public String makeGood(String s) {
115+
StringBuilder result = new StringBuilder();
116+
117+
for (char c : s.toCharArray()) {
118+
if (result.length() == 0) {
119+
result.append(c);
120+
} else {
121+
char lastChar = result.charAt(result.length() - 1);
122+
if (Character.toLowerCase(c) == Character.toLowerCase(lastChar)
123+
&& c != lastChar + 32 && c != lastChar - 32) {
124+
result.deleteCharAt(result.length() - 1);
125+
} else {
126+
result.append(c);
127+
}
128+
}
129+
}
130+
131+
return result.toString();
132+
}
133+
}
134+
```
135+
136+
#### C++
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
string makeGood(std::string s) {
142+
string result ;
143+
for (int i=0 ; i< s.length() ; i++)
144+
{
145+
if (result.empty())
146+
{
147+
result += s[i];
148+
}
149+
150+
else
151+
{
152+
if (!result.empty() && (s[i] == result.back() - 32 || s[i] == result.back() + 32))
153+
{
154+
result.erase(result.size() - 1);
155+
}
156+
157+
else if (!result.empty())
158+
{
159+
result += s[i] ;
160+
}
161+
}
162+
}
163+
164+
return result;
165+
166+
}
167+
};
168+
```
169+
170+
### Conclusion
171+
172+
The code efficiently creates a "good" string by removing adjacent pairs of characters where one is the uppercase version of the other. It uses a StringBuilder to store and manipulate the characters, iterating through the input string and checking for adjacent pairs. If such a pair is found, the last character is removed from the StringBuilder; otherwise, the current character is appended. This approach ensures that the resulting string has no adjacent pairs of uppercase-lowercase characters, adhering to the desired output. The time complexity of this approach is $O(n)$, where $n$ is the length of the input string, as each character is processed once. The space complexity is also $O(n)$ due to the storage of the StringBuilder.

0 commit comments

Comments
 (0)