Skip to content

T I Double Guh Er II

Raymond Chen edited this page Aug 1, 2024 · 4 revisions

TIP102 Unit 1 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 5 mins
  • 🛠️ Topics: Strings, Substring Replacement

U-nderstand

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?
  • The function tiggerfy() should take a string word and return a new string with the substrings t, i, gg, and er removed. The function should be case insensitive.
HAPPY CASE
Input: `Trigger`
Expected Output: `r`
Explanation: Removing `t`, `i`, `gg`, and `er` from `Trigger` results in `r`.

Input: `eggplant`
Expected Output: `eplan`
Explanation: Removing `g` from `eggplant` results in `eplan`.

EDGE CASE
Input: `Choir`
Expected Output: `Choir`
Explanation: No specified substrings are present in `Choir`, so it remains unchanged.

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Convert the string to lowercase to ensure case insensitivity, then iteratively replace each specified substring with an empty string.

1. Define the function `tiggerfy(word)`.
2. Convert the `word` to lowercase to handle case insensitivity.
3. Replace the substrings `t`, `i`, `gg`, and `er` with an empty string in the `word`.
4. Return the modified `word`.

⚠️ Common Mistakes

  • Forgetting to handle case insensitivity by not converting the input to lowercase.
  • Not replacing substrings in the correct order, which may cause issues if larger substrings are part of smaller ones.

I-mplement

Implement the code to solve the algorithm.

def tiggerfy(word):
    # Convert the word to lowercase to handle case insensitivity
    word_lower = word.lower()
    
    # Replace the specified substrings with an empty string
    word_lower = word_lower.replace('t', '')
    word_lower = word_lower.replace('i', '')
    word_lower = word_lower.replace('gg', '')
    word_lower = word_lower.replace('er', '')
    
    return word_lower
Clone this wiki locally