Codepath

T I Double Guh Er II

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.

  • Q: What is the input to the function?

    • A: The input is a string word that may contain substrings "t", "i", "gg", or "er" that need to be removed.
  • Q: What is the expected output of the function?

    • A: The function should return a new string with the substrings "t", "i", "gg", and "er" removed.
  • Q: How should the function handle case sensitivity?

    • A: The function should be case insensitive, meaning "T", "I", "GG", and "ER" should also be removed if present.
  • Q: What if the string is empty or does not contain any of the specified substrings?

    • A: If the string is empty, the function should return an empty string. If none of the specified substrings are present, the function should return the original string, converted to lowercase.
  • 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
Fork me on GitHub