LeetCode 2823: Deep Object Filter Solution
Master LeetCode problem 2823 (Deep Object Filter), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
2823. Deep Object Filter
Problem Explanation
Explanation:
To solve this problem, we need to filter a nested object based on a provided filter object. We will recursively traverse the nested object and filter out the keys that do not match the filter object. If a key matches and its value is another object, we will recursively filter that object as well.
- Recursively traverse the nested object.
- For each key in the object:
- If the key is present in the filter object and its value matches, keep it.
- If the value is an object, recursively filter the nested object.
- Otherwise, ignore the key.
- Return the filtered object.
Time Complexity: O(N), where N is the total number of keys in the nested object.
Space Complexity: O(N), due to the recursion stack.
:
Solution Code
class Solution {
public Map<String, Object> deepObjectFilter(Map<String, Object> obj, Map<String, Object> filter) {
Map<String, Object> result = new HashMap<>();
for (Map.Entry<String, Object> entry : obj.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (filter.containsKey(key) && filter.get(key).equals(value)) {
if (value instanceof Map) {
result.put(key, deepObjectFilter((Map<String, Object>) value, (Map<String, Object>) filter.get(key));
} else {
result.put(key, value);
}
}
}
return result;
}
}Try It Yourself
Loading code editor...
Frequently Asked Questions
How to solve LeetCode 2823 (Deep Object Filter)?
This page provides optimized solutions for LeetCode problem 2823 (Deep Object Filter) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2823 (Deep Object Filter)?
The time complexity for LeetCode 2823 (Deep Object Filter) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2823 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2823 in Java, C++, or Python.