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 - 2s[i]is a lower-case letter ands[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.
Example 1:
1Input: s = "leEeetcode"
2Output: "leetcode"
3Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
Example 2:
1Input: s = "abBAcC"
2Output: ""
3Explanation: We have many possible scenarios, and all lead to the same answer. For example:
4"abBAcC" --> "aAcC" --> "cC" --> ""
5"abBAcC" --> "abBA" --> "aA" --> ""
Example 3:
1Input: s = "s"
2Output: "s"
Constraints:
1 <= s.length <= 100scontains only lower and upper case English letters.
Solution
Using Recursion
We can use recursion to solve this problem. We iterate from start to end and every time we find two consecutive characters being same, you use the recursive call to the same method but skipping these two same characters.
1class Solution {
2 public String makeGood (String s) {
3 for (int i = 0; i < s.length() - 1; i++) {
4 if (Math.abs(s.charAt(i) - s.charAt(i + 1)) == 32) {
5 return makeGood(s.substring(0, i) + s.substring(i + 2)); // skip these two characters
6 }
7 }
8 return s;
9 }
10}
- Time Complexity:
O(n^2). It is recursively finding two same characters to remove which takesO(n^2)time complexity. - Space Complexity:
O(n^2)- In this case, you have two function calls in each iteration. Each function call requiresO(n)space. Thus overall space complexity isn^2.
Using Stack
You can also use stack to push each character. Every time, we check new character, we will peek at the stack for last inserted character. If they are same ignoring case, then we can pop the last inserted element.
1class Solution {
2 public String makeGood(String s) {
3 Stack<Character> stack = new Stack<>();
4 for (char c : s.toCharArray()) {
5 if (!stack.isEmpty() && Math.abs(stack.peek() - c) == 32) {
6 stack.pop();
7 } else {
8 stack.push(c);
9 }
10 }
11
12 StringBuilder sb = new StringBuilder();
13 for (char c : stack) {
14 sb.append(c);
15 }
16
17 return sb.toString();
18 }
19}
- Time Complexity:
O(n) - Space Complexity:
O(n)
Using Iteration or StringBuilder
With StringBuilder you don’t need to create new String everytime. StringBuilder is mutable.
1class Solution {
2 public String makeGood (String s) {
3 StringBuilder sb = new StringBuilder();
4 for (char c : s.toCharArray()) {
5 if (sb.length() > 0 && Math.abs(sb.charAt(sb.length() - 1) - c) == 32) {
6 sb.deleteCharAt(sb.length() - 1);
7 } else {
8 sb.append(c);
9 }
10 }
11 return sb.toString();
12 }
13}
- Time Complexity:
O(n) - Space Complexity:
O(n)


Comments