You are viewing a single comment's thread. Return to all comments →
My Solution Using Java
Time Complexity: 𝑂(𝑛)
Space Complexity: 𝑂(𝑛)
import java.util.*;
public static String superReducedString(String s) { Stack stack = new Stack<>();
for (int i = 0; i < s.length(); i++) { char currentChar = s.charAt(i); if (!stack.isEmpty() && stack.peek() == currentChar) { stack.pop(); } else { stack.push(currentChar); } } if (stack.isEmpty()) { return "Empty String"; } StringBuilder reducedString = new StringBuilder(); for (char c : stack) { reducedString.append(c); } return reducedString.toString();
}
Seems like cookies are disabled on this browser, please enable them to open this website
Super Reduced String
You are viewing a single comment's thread. Return to all comments →
My Solution Using Java
Time Complexity: 𝑂(𝑛)
Space Complexity: 𝑂(𝑛)
import java.util.*;
public static String superReducedString(String s) { Stack stack = new Stack<>();
}