469. Find Entry and Exit Violations in Office Attendance Log
Find Entry and Exit Violations in Office Attendance Log

You are given the attendance log of an office as a list of records in the order they happened. Each record says that an employee, identified by a unique name, either entered or exited the office.

Every employee must follow the flow enter → exit → enter → exit → .... The office is empty when the log starts, and every employee must have exited by the time the log ends. Looking only at the records of one employee:

  • An entry violation happens when an enter is not followed by an exit, i.e. the next record of that employee is also enter (enter → enter) or there is no next record.
  • An exit violation happens when an exit is not preceded by an enter, i.e. the previous record of that employee is also exit (exit → exit) or there is no previous record.

Find the names of all employees with entry violations and all employees with exit violations.

Method Signature

List<List<String>> findViolations(List<String> records)
  • Each record is a string "name,action", where action is "enter" or "exit", e.g. "Tara,enter".
  • Return a list of exactly two lists:
    • index 0: names of employees with an entry violation.
    • index 1: names of employees with an exit violation.
  • Each list contains distinct names sorted in ascending lexicographical order, and is empty if no employee has that violation.
  • A name can appear in both lists.

Constraints

  • 1 ≤ records.size() ≤ 100,000
  • 1 ≤ name.length() ≤ 20
  • The first letter of name is an uppercase English letter and all other letters are lowercase English letters.
  • action is either "enter" or "exit".

Examples

Example 1

Input: findViolations(records = ["Rohan,exit", "Tara,enter", "Vikram,enter", "Tara,enter", "Pooja,enter", "Vikram,exit", "Pooja,exit", "Tara,exit", "Neha,enter", "Pooja,exit", "Rohan,enter", "Rohan,exit"])

Output: [["Neha", "Tara"], ["Pooja", "Rohan"]]

Explanation:

  • Tara: enter → enter → exit. It has enter → enter, so entry violation.
  • Neha: enter. She is still inside when the log ends, so entry violation.
  • Rohan: exit → enter → exit. His first record is exit, so exit violation.
  • Pooja: enter → exit → exit. It has exit → exit, so exit violation.
  • Vikram: enter → exit. No violation.

Example 2

Input: findViolations(records = ["Kiran,enter", "Manav,enter", "Kiran,exit", "Kiran,enter", "Manav,exit", "Kiran,exit"])

Output: [[], []]

Explanation: Kiran (enter → exit → enter → exit) and Manav (enter → exit) both follow the correct flow, so both lists are empty.

Example 3

Input: findViolations(records = ["Zoya,exit", "Zoya,enter", "Zoya,enter", "Zoya,exit", "Zoya,exit"])

Output: [["Zoya"], ["Zoya"]]

Explanation: Zoya's records are exit → enter → enter → exit → exit. The enter → enter part is an entry violation. The first exit and the exit → exit part are exit violations. Her name appears only once in each list.



Please use Laptop/Desktop or any other large screen to add/edit code.