1108. Defanging an IP Address
Problem
Given a valid IPv4 IP address, return a defanged version where every
"." is replaced with "[.]".Example:
- Input:
"1.1.1.1"Output:"1[.]1[.]1[.]1"
- Input:
"255.100.50.0"Output:"255[.]100[.]50[.]0"
Approach
- Traverse the string.
- For each character:
- If it is
., append[.]to the result. - Else, append the character itself.
Code
Time Complexity:
O(n) Space Complexity:
O(n) 1109. Corporate Flight Bookings
Problem
There are
n flights labelled from 1 to n. Each booking is [first, last, seats] representing seats booked for all flights from first to last.Return a list of total seats booked for each flight.
Example:
- Input:
bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
Output:
[10,55,45,25,25]Approach
- Use a difference array:
- Add
seatsatstart-1. - Subtract
seatsatend(ifend < n).
- Compute prefix sum to get actual seats for each flight.
Code
Time Complexity:
O(n + m) (m = number of bookings)Space Complexity:
O(n)1110. Delete Nodes And Return Forest
Problem
Given a binary tree root and a list of nodes
to_delete, remove these nodes and return the forest of remaining trees.Example:
- Input:
root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output:
[[1,2,null,4],[6],[7]]Approach
- Use BFS/DFS traversal.
- Track nodes to delete using a set or array.
- If a node is deleted:
- Detach it from parent.
- Its children (if not deleted) become new roots.
- Collect all new roots in a result vector.
Code
Time Complexity:
O(n)Space Complexity:
O(n) 1111. Maximum Nesting Depth of Two Valid Parentheses Strings
Problem
Given a valid parentheses string
seq, split it into two disjoint valid strings A and B such that maximum depth of either is minimized.Return an array where
res[i] = 0 if seq[i] is part of A, else 1.Example:
- Input:
"(()())"→ Output:[0,1,1,1,1,0]
Approach
- Use a stack or depth counter:
- Keep track of nesting depth.
- Alternate assignment of
'('and')'toAandBto minimize max depth.
Code
Time Complexity:
O(n)Space Complexity:
O(n) 