JCSU Unit 5 Problem Set 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE Input: s = "aabbccdde" Output: "abcde" Explanation: Adjacent duplicates "aa", "bb", "cc", and "dd" are reduced to "a", "b", "c", and "d".
EDGE CASE Input: s = "" Output: "" Explanation: An empty string results in an empty output.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For adjacent duplicate removal problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Iterate through the string and compare each character with the previous one. If the current character is different from the previous one, add it to the result list.
s
is empty. If it is, return an empty string.result
with the first character of the string.result
.result
back into a string using join()
and return it.Implement the code to solve the algorithm.
def remove_adjacent_duplicates(s):
if not s: # Check if the string is empty
return "" # Return an empty string if the input is empty
result = [s[0]] # Initialize the result list with the first character
for i in range(1, len(s)): # Iterate through the string starting from the second character
if s[i] != s[i - 1]: # Compare the current character with the previous one
result.append(s[i]) # Add the current character if it's not a duplicate
return ''.join(result) # Join the result list into a string and return it
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
Example 2:
Example 3:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of the string.