Untitled
unknown
plain_text
2 months ago
1.8 kB
2
Indexable
class Solution { public String removeOccurrences(String s, String part) { Stack<Character> stk = new Stack<>(); int strLength = s.length(); int partLength = part.length(); // Iterate through each character in the string for (int index = 0; index < strLength; index++) { // Push current character to stack stk.push(s.charAt(index)); // If stack size is greater than or equal to the part length, check for match if (stk.size() >= partLength && checkMatch(stk, part, partLength)) { // Pop the characters matching 'part' from the stack for (int j = 0; j < partLength; j++) { stk.pop(); } } } // Convert stack to string with correct order StringBuilder result = new StringBuilder(); while (!stk.isEmpty()) { result.append(stk.pop()); } result.reverse(); return result.toString(); } // Helper function to check if the top of the stack matches the 'part' private boolean checkMatch( Stack<Character> stk, String part, int partLength ) { Stack<Character> temp = new Stack<>(); temp.addAll(stk); // Copy the stack to avoid modifying the original // Iterate through part from right to left for (int partIndex = partLength - 1; partIndex >= 0; partIndex--) { // If current stack top doesn't match expected character if (temp.isEmpty() || temp.peek() != part.charAt(partIndex)) { return false; } temp.pop(); } return true; } }
Editor is loading...
Leave a Comment