You are given trace logs from a program. Each log records either entering a function or returning from a function. While scanning the logs from left to right, maintain the active call stack. Every time a function is entered, push its name onto the stack, and every time a function returns, pop the top of the stack. Your task is to determine the most frequently occurring function call stack.
Given a list of strings logs where each string is either a function name or a function return, represented as follows:
"{function}": A function name."{function}:return": A function return.Your goal is to return the most frequently occurring stack of function names. If there are multiple stacks with the same frequency, return the one that appears first in the logs.
1 <= logs.length <= 1001 <= logs[i].length <= 100logs[i] consists of lowercase English letters and digits."{function1}" and "{function2}" will not both appear as two consecutive elements in logs.Input:
logs = ["a","a:f","c","a:f","c:d","a:f:b:d"]
Output:
"a:f:b:d"
Explanation:
The most frequent stack with the highest frequency is ["a","f","b","d"].
Input:
logs = ["a","c","b","b:c","a:c","b:c:d"]
Output:
"b:c:d"
Explanation:
The most frequent stack with the highest frequency is ["b","c","d"].
Here is a possible solution in Python: `python from collections import defaultdict
def mostFrequentStack/logs): stack = [] freq = defaultdict(int) max_freq = 0 most_freq_stack = []
for log in logs:
if log.endswith(":return"):
stack.pop()
else:
stack.append(log)
curr_stack = ":".join(stack)
freq[curr_stack] += 1
if freq[curr_stack] > max_freq:
max_freq = freq[curr_stack]
most_freq_stack = curr_stack
return most_freq_stack
logs = ["a","a:f","c","a:f","c:d","a:f:b:d"] print(mostFrequentStack(logs)) # Output: "a:f:b:d" `
This solution iterates through each log entry, updating the current stack and the frequency of each stack. It keeps track of the maximum frequency and the corresponding stack, which is the most frequent stack.