Unit 7 Session 2 (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 = "banana", char = "a"
Output: "bnn"
Explanation: All instances of 'a' are removed from the string.
EDGE CASE
Input: s = "aaaaa", char = "a"
Output: "
Explanation: Removing 'a' from a string of only 'a's results in an empty string.
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.
This problem is a straightforward application of recursive string manipulation:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use recursion to process each character of the string, removing the specified character and building the result recursively.
1) If the string is empty, return an empty string.
2) If the first character of the string is the character to remove, skip it and recursively process the rest of the string.
3) Otherwise, include the first character and recursively process the rest of the string.
4) Combine the results of the recursive calls to build the final string.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def remove_char(s, char):
if not s:
return "
if s[0] == char:
return remove_char(s[1:], char)
else:
return s[0] + remove_char(s[1:], char)
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(n)
where n
is the length of the string since each character is processed once.O(n)
for the recursion stack in the worst case where no characters are removed.