-
Notifications
You must be signed in to change notification settings - Fork 272
T I Double Guh Er II Big O Analysis
DSA for TIP Unit 4 Problem Set 1 (Click for link to problem statements)
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20-30 mins
- 🛠️ Topics: Complexity Analysis, String Manipulation, Regex Optimization
Understand what the interviewer is asking for by analyzing the behavior and efficiency of the function
tiggerfy().
Questions:
- What is the worst-case time complexity of
tiggerfy()during iterative substring removal? - What is the space complexity of
tiggerfy()when creating new strings during each removal? - Would using
re.sub()to replace substrings in one pass improve the time complexity?
-
tiggerfy()processes a stringwordand removes specific substrings ("t","i","gg","er") iteratively or in one pass. - String manipulations such as slicing or replacement typically involve creating new strings, which affects space complexity.
Match this problem to known complexity analysis concepts and scenarios.
-
Iterative Substring Removal:
- Each
replace()call scans the string once and removes every occurrence of its pattern (str.replacereplaces all occurrences by default). - One pass is made per pattern, and the number of patterns (4) is a fixed constant.
- Each
-
Regex-Based Replacement (
re.sub):- Processes the entire string in a single pass using a compiled regex pattern.
- Optimized for scenarios involving multiple pattern replacements.
Plan the analysis by breaking down the function's behavior step by step.
- Analyze the cost of iterative substring removal.
- Evaluate the impact of repeated iterations on complexity.
- Identify auxiliary memory usage and the cost of creating new strings.
- Compare iterative and regex-based approaches.
Implement the analysis with clear justifications.
-
Iterative Removal:
- Each
replace()call scans the string and builds the result in a single pass, which takes (O(m)), where (m) is the current string length. - With (k) patterns to remove, the total complexity is (O(k \cdot m)); here (k = 4) is a fixed constant, so the overall time complexity is (O(m)).
- Even a string made entirely of removable characters (e.g.,
"tttttt") is cleared in the singlereplace('t', '')pass, sincereplace()removes all occurrences at once.
- Each
-
Regex Replacement (
re.sub):- Replacing all substrings in one pass using
re.sub()involves (O(m)), as the regex engine processes the string sequentially.
- Replacing all substrings in one pass using
-
Iterative Removal:
- Each removal creates a new string, requiring additional memory proportional to the string length.
- Space complexity is (O(m)), where (m) is the original string length.
-
Regex Replacement (
re.sub):- A new string is created during replacement, resulting in (O(m)) space complexity.
- No significant auxiliary data structures are used.
-
Advantages:
- Processes the input string in one pass.
- Time complexity stays (O(m)), matching the four-
replace()version; the gain is a constant factor (one pass instead of four), not an asymptotic improvement. - Cleaner and more maintainable code for multiple substring replacements.
-
Disadvantages:
- Requires importing and learning regex (
remodule). - Slight overhead for compiling the regex pattern.
- Requires importing and learning regex (
Review the scenarios and validate with examples.
-
Input:
word = "tiggeriggerrr"- Iterative Approach (each step removes every occurrence of its pattern):
- Remove
"t"→"iggeriggerrr". - Remove
"i"→"ggerggerrr". - Remove
"gg"→"ererrr". - Remove
"er"→"rr". - Four (O(m)) passes: (O(m)) overall.
- Remove
- Regex Approach:
- Single pass removes all substrings: (O(m)).
- Iterative Approach (each step removes every occurrence of its pattern):
-
Input:
word = "nonremovable"- Both approaches process the string once and exit quickly: (O(m)).
Evaluate the performance of
tiggerfy()and the trade-offs between iterative and regex-based implementations.
-
Iterative Approach:
- Time Complexity: (O(m)) — one full-string pass per pattern, with a constant number of patterns.
- Space Complexity: (O(m)) for creating new strings.
-
Regex-Based Replacement:
- Time Complexity: (O(m)), as all substrings are replaced in one pass.
- Space Complexity: (O(m)), as a new string is created.
-
Iterative Approach:
- Simpler for small strings or few substrings.
- Makes one full pass per pattern, so the constant factor grows with the number of patterns.
-
Regex Replacement:
- More efficient for large strings or complex replacement patterns.
- Requires understanding and using regex syntax.