~/coding-interview/leetcode-crawler-log-folder.md

Crawler Log Folder - Leet Code Solution

2 min readOctober 03, 2020by Gorav Singal#cracking-the-coding-interview#coding-problems#leetcode
// TL;DR

Track depth with a counter — '../' decrements (min 0), './' does nothing, anything else increments. Final counter value is the answer.

Crawler Log Folder - Leet Code Solution

Problem

The Leetcode file system keeps a log each time some user performs a change folder operation.

The operations are described below:

”../” : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder). “./” : Remain in the same folder. “x/” : Move to the child folder named x (This folder is guaranteed to always exist). You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.

The file system starts in the main folder, then the operations in logs are performed.

Return the minimum number of operations needed to go back to the main folder after the change folder operations.

Example

Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to the main folder.

Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3

Input: logs = ["d1/","../","../","../"]
Output: 0

Solution

This is fairly simple problem. The only complexity in this problem is that for ../. If you are in the main folder, and you do a ../, you should remain in the same folder.

If you see, the problem is asking you to calculate how far you have gone, or the distance. Because, that is how many steps you will require to come back to original main folder.

Lets try to calculate the distance from the main folder.

  • Keep an integer variable for calculating distance
  • If the string is ./, Just keep the counter as it is
  • If its ../, you need to check if you are in the current folder or not. If you are in the current folder, your distance counter will be zero. So if its zero, do nothing else decrement the counter
  • Now the third operation is changing to a folder, i.e. abc/, just increment the counter.

Lets look at the code

public int minOperations(String[] logs) {
  int distance = 0;
  for (int i=0; i<logs.length; i++) {
    if (logs[i].equals("./")) {
      continue;
    }
    else if (logs[i].equals("../")) {
      if (distance == 0) {
        //do nothing
        continue;
      }
      else {
        distance --;
      }
    }
    else {
      // folder/
      distance ++;
    }
  }
  
  return distance;
  }

Complexity

Its simple O(n) where n is the length of array

Leetcode Submission Result

Your runtime beats 100% of java submissions. Memory usage beats 80% of java submissions.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Leetcode Solution - Best Time to Buy and Sell Stock

Leetcode Solution - Best Time to Buy and Sell Stock

Problem Statement You are given an array prices where prices[i] is the price of…

Binary Tree - Level Order Traversal

Binary Tree - Level Order Traversal

Problem Statement Given a Binary tree, print out nodes in level order traversal…

Four Sum - Leet Code Solution

Four Sum - Leet Code Solution

Problem Statement Given an array nums of n integers and an integer target, are…

Leetcode - Rearrange Spaces Between Words

Leetcode - Rearrange Spaces Between Words

Problem Statement You are given a string text of words that are placed among…

Leetcode - Maximum Non Negative Product in a Matrix

Leetcode - Maximum Non Negative Product in a Matrix

Problem Statement You are given a rows x cols matrix grid. Initially, you are…

Leetcode - Split a String Into the Max Number of Unique Substrings

Leetcode - Split a String Into the Max Number of Unique Substrings

Problem Statement Given a string s, return the maximum number of unique…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…