Infosys | OA | Minimum No of Directories Traversed
Anonymous User
226

Given a directory path, find out the minimum number of directories are
needed to be traversed from the current location to the root location.

  • The path contains only directories, not files.
  • Root Location is '/'
  • Given a path /opt/tournaments/ipl/2020/matches, the number of directories to be traversed to be traversed to travel back to the root are 5 (opt, tournaments, ipl, 2020, matches)

The path given can contain

  1. Directory Name
  2. . (single dot)
  3. .. (double dot)

Input Format: Single line with the entire path

Output Format: An integer denoting the minimum number of directories to be traversed to go back to the root

Example-1:
Input: /opt/tournaments/ipl/2020/matches
Output : 5

Example-2:
Input: /opt/tournaments/./ipl/../odi-wc-men/2019/matches/../venues
Output : 5
Explanation: The final path to be traversed is /opt/tournaments/odi-wc-men/2019/venues1

Comments (1)