-
-
Notifications
You must be signed in to change notification settings - Fork 155
Adding LC solution for 1544. Make The String Great #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ajay-dhangar
merged 6 commits into
codeharborhub:main
from
aaradhyasinghgaur:adding-lc-solution2
Jun 9, 2024
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
70878b2
Add files via upload
aaradhyasinghgaur cfd935c
Merge branch 'CodeHarborHub:main' into adding-lc-solution2
aaradhyasinghgaur e1f2fa0
Update 1544-make-the-string-great.md
aaradhyasinghgaur 980f928
Update 1544-make-the-string-great.md
aaradhyasinghgaur 8f694ac
Update 1544-make-the-string-great.md
aaradhyasinghgaur 43fb210
Update 1544-make-the-string-great.md
aaradhyasinghgaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
--- | ||
id: make-the-string-great | ||
title: Make The String Great (Leetcode) | ||
sidebar_label: 1544-MakeTheStringGreat | ||
description: Given a string s of lower and upper case English letters. | ||
--- | ||
|
||
## Problem Description | ||
|
||
| Problem Statement | Solution Link | LeetCode Profile | | ||
| :---------------- | :------------ | :--------------- | | ||
| [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/) | | ||
|
||
|
||
## Problem Description | ||
|
||
Given a string $s$ of lower and upper case English letters. | ||
|
||
A good string is a string which doesn't have two adjacent characters $s[i]$ and $s[i + 1]$ where: | ||
|
||
- $0 <= i <= s.length - 2$ | ||
- $s[i]$ is a lower-case letter and $s[i + 1]$ is the same letter but in upper-case or vice-versa. | ||
|
||
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. | ||
|
||
Return the string after making it good. The answer is guaranteed to be unique under the given constraints. | ||
|
||
Notice that an empty string is also good. | ||
|
||
### Examples | ||
|
||
#### Example 1 | ||
|
||
- **Input:** s = "leEeetcode" | ||
- **Output:** $"leetcode"$ | ||
- **Explanation:** In the first step, either you choose $i = 1$ or $i = 2$, both will result $"leEeetcode"$ to be reduced to $"leetcode"$. | ||
|
||
|
||
#### Example 2 | ||
|
||
- **Input:** $s = "abBAcC"$ | ||
- **Output:** $""$ | ||
- **Explanation:** We have many possible scenarios, and all lead to the same answer. For example:</br> | ||
"abBAcC" --> "aAcC" --> "cC" --> "" | ||
"abBAcC" --> "abBA" --> "aA" --> "" | ||
|
||
#### Example 2 | ||
|
||
- **Input:** s = "s" | ||
- **Output:** "s" | ||
|
||
|
||
### Constraints | ||
|
||
|
||
- $1 <= s.length <= 100$ | ||
- s contains only lower and upper case English letters. | ||
|
||
|
||
|
||
### Intuition | ||
|
||
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. | ||
|
||
### Approach | ||
|
||
1. **Initialize an Empty String:** | ||
|
||
- Initialize an empty string result to store the characters after processing. | ||
|
||
2. **Iterate Through the Input String:** | ||
|
||
- Iterate through each character in the input string s using a for loop. | ||
|
||
3. **Check for Empty String:** | ||
|
||
- If result is empty, add the current character to result. | ||
|
||
4. **Check for Adjacent Pairs:** | ||
|
||
- 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'). | ||
- If such a pair is found, remove the last character from result using result.erase(result.size() - 1). | ||
- If no adjacent pair is found, append the current character to result. | ||
|
||
5. **Return the Resulting String:** | ||
|
||
- After processing all characters in s, return the final value of result, which represents the "good" string without adjacent pairs of uppercase-lowercase characters. | ||
|
||
### Solution Code | ||
|
||
#### Python | ||
|
||
```py | ||
class Solution: | ||
def makeGood(self, s: str) -> str: | ||
result = [] | ||
|
||
for char in s: | ||
if not result: | ||
result.append(char) | ||
else: | ||
if result[-1].lower() == char.lower() and (result[-1] != char): | ||
result.pop() | ||
else: | ||
result.append(char) | ||
|
||
return ''.join(result) | ||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
class Solution { | ||
public String makeGood(String s) { | ||
StringBuilder result = new StringBuilder(); | ||
|
||
for (char c : s.toCharArray()) { | ||
if (result.length() == 0) { | ||
result.append(c); | ||
} else { | ||
char lastChar = result.charAt(result.length() - 1); | ||
if (Character.toLowerCase(c) == Character.toLowerCase(lastChar) | ||
&& c != lastChar + 32 && c != lastChar - 32) { | ||
result.deleteCharAt(result.length() - 1); | ||
} else { | ||
result.append(c); | ||
} | ||
} | ||
} | ||
|
||
return result.toString(); | ||
} | ||
} | ||
``` | ||
|
||
#### C++ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
string makeGood(std::string s) { | ||
string result ; | ||
for (int i=0 ; i< s.length() ; i++) | ||
{ | ||
if (result.empty()) | ||
{ | ||
result += s[i]; | ||
} | ||
|
||
else | ||
{ | ||
if (!result.empty() && (s[i] == result.back() - 32 || s[i] == result.back() + 32)) | ||
{ | ||
result.erase(result.size() - 1); | ||
} | ||
|
||
else if (!result.empty()) | ||
{ | ||
result += s[i] ; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
}; | ||
``` | ||
|
||
### Conclusion | ||
|
||
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.