Branch with Most Files
Problem statement
A simple version-control system processes the strings in logs in order. Each branch stores its own set of file names.
switch <branch>selects that branch. A branch encountered for the first time starts empty; switching back preserves its files.push <file>adds the file name to the current branch. Pushing a file already present in that branch does not increase its number of files.
Return the name of the branch containing the most distinct files after all operations. The maximum is unique. The first operation is switch, and at least one push occurs.
Names are case-sensitive and contain no whitespace. Every command has exactly one space between its operation and name.
Function
branchWithMostFiles(logs: String[]) → StringExamples
Example 1
logs = ["switch main","push a","push a","switch dev","push x","push y"]return = "dev"main has one distinct file, a. dev has two, x and y.
Example 2
logs = ["switch root","push z","switch empty","switch root","push w"]return = "root"Returning to root preserves z, then adds w. The other branch remains empty.
Constraints
2 <= logs.length <= 1000- Each branch or file name contains
1to50ASCII letters, digits, underscores, periods, or hyphens. - Every command follows one of the two specified forms.
- The first command is
switch, at least one command ispush, and exactly one branch has the largest final file count.