From 70878b2394bcd5b405a1374872931adabf5b4e9d Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+aaradhyasinghgaur@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:20:49 +0530 Subject: [PATCH 1/5] Add files via upload --- .../1500-1599/1544-make-the-string-great.md | 183 ++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md diff --git a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md new file mode 100644 index 000000000..5b525b673 --- /dev/null +++ b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md @@ -0,0 +1,183 @@ +--- +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. + +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. +--- + +## 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:
+$"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. \ No newline at end of file From e1f2fa00ff4f0c254f43fee1b50da5b8fa6329f1 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+aaradhyasinghgaur@users.noreply.github.com> Date: Sun, 9 Jun 2024 15:05:36 +0530 Subject: [PATCH 2/5] Update 1544-make-the-string-great.md --- .../1500-1599/1544-make-the-string-great.md | 25 ++++++------------- 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md index 5b525b673..deccbbb7a 100644 --- a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md +++ b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md @@ -3,17 +3,6 @@ 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. - -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. --- ## Problem Description @@ -42,7 +31,7 @@ Notice that an empty string is also good. #### Example 1 -- **Input:** $s = "leEeetcode"$ +- **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"$. @@ -52,20 +41,20 @@ Notice that an empty string is also good. - **Input:** $s = "abBAcC"$ - **Output:** $""$ - **Explanation:** We have many possible scenarios, and all lead to the same answer. For example:
-$"abBAcC"$ --> $"aAcC"$ --> $"cC"$ --> $""$ -$"abBAcC"$ --> $"abBA"$ --> $"aA"$ --> $""$ +"abBAcC" --> "aAcC" --> "cC" --> "" +"abBAcC" --> "abBA" --> "aA" --> "" #### Example 2 -- **Input:** $s = "s"$ -- **Output:** $"s"$ +- **Input:** s = "s" +- **Output:** "s" ### Constraints - $1 <= s.length <= 100$ -- $s$ contains only lower and upper case English letters. +- s contains only lower and upper case English letters. @@ -180,4 +169,4 @@ public: ### 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. \ No newline at end of file +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. From 980f92844513d28562c336142f65a71d7fbab9ec Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+aaradhyasinghgaur@users.noreply.github.com> Date: Sun, 9 Jun 2024 15:11:59 +0530 Subject: [PATCH 3/5] Update 1544-make-the-string-great.md --- .../lc-solutions/1500-1599/1544-make-the-string-great.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md index deccbbb7a..d16a16c73 100644 --- a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md +++ b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md @@ -19,7 +19,7 @@ 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. +- 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. @@ -32,7 +32,7 @@ Notice that an empty string is also good. #### Example 1 - **Input:** s = "leEeetcode" -- **Output:** $"leetcode"$ +- **Output:** "leetcode" - **Explanation:** In the first step, either you choose $i = 1$ or $i = 2$, both will result $"leEeetcode"$ to be reduced to $"leetcode"$. From 8f694ac69f07c6e76a7d89937b741a4735023360 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+aaradhyasinghgaur@users.noreply.github.com> Date: Sun, 9 Jun 2024 15:12:46 +0530 Subject: [PATCH 4/5] Update 1544-make-the-string-great.md --- .../lc-solutions/1500-1599/1544-make-the-string-great.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md index d16a16c73..05c3f01c9 100644 --- a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md +++ b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md @@ -38,8 +38,8 @@ Notice that an empty string is also good. #### Example 2 -- **Input:** $s = "abBAcC"$ -- **Output:** $""$ +- **Input:** s = "abBAcC" +- **Output:** "" - **Explanation:** We have many possible scenarios, and all lead to the same answer. For example:
"abBAcC" --> "aAcC" --> "cC" --> "" "abBAcC" --> "abBA" --> "aA" --> "" From 43fb210e605f3aa7c0eed1321fad5ce00b258f58 Mon Sep 17 00:00:00 2001 From: Aaradhya_Singh <123281503+aaradhyasinghgaur@users.noreply.github.com> Date: Sun, 9 Jun 2024 15:15:08 +0530 Subject: [PATCH 5/5] Update 1544-make-the-string-great.md --- .../lc-solutions/1500-1599/1544-make-the-string-great.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md index 05c3f01c9..8f1b7fd22 100644 --- a/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md +++ b/dsa-solutions/lc-solutions/1500-1599/1544-make-the-string-great.md @@ -40,7 +40,7 @@ Notice that an empty string is also good. - **Input:** s = "abBAcC" - **Output:** "" -- **Explanation:** We have many possible scenarios, and all lead to the same answer. For example:
+- **Explanation:** We have many possible scenarios, and all lead to the same answer. For example:
"abBAcC" --> "aAcC" --> "cC" --> "" "abBAcC" --> "abBA" --> "aA" --> ""