FastPrepBranch with Most Files

Branch with Most Files

Hudson River Trading logoHudson River Trading● EasyFULLTIMEOA
Learn

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[]) → String

Examples

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 1 to 50 ASCII letters, digits, underscores, periods, or hyphens.
  • Every command follows one of the two specified forms.
  • The first command is switch, at least one command is push, and exactly one branch has the largest final file count.

More Hudson River Trading problems

See Hudson River Trading hiring insights
public String branchWithMostFiles(String[] logs) {
    // Write your code here.
}
logs["switch main","push a","push a","switch dev","push x","push y"]
expected"dev"
Checking account…