703125. Find the number of tabs opened

Easy

Here is the Markdown blog post for the problem:

Tiger Zinda Hai: Find the number of tabs opened

Summary

In this problem, we are asked to find the total number of tabs opened in a system. Given an array of strings where each string represents a tab opening or closing operation (e.g., "Open" or "Close"), our task is to count the total number of tabs opened. This problem involves arrays and hash data structures.

Detailed Explanation

The approach to this problem is simple yet effective. We iterate through the array of operations, keeping track of the current number of open tabs using a variable open_tabs. Whenever we encounter an "Open" operation, we increment open_tabs, and whenever we encounter a "Close" operation, we decrement it. The final value of open_tabs will represent the total number of tabs opened.

Here is a step-by-step breakdown of the solution:

  1. Initialize a variable open_tabs to 0.
  2. Iterate through the array of operations.
  3. For each operation, check if it's an "Open" or "Close" operation.
  4. If it's an "Open" operation, increment open_tabs.
  5. If it's a "Close" operation and open_tabs is greater than 0, decrement open_tabs. This ensures that we don't go below 0 (i.e., negative tabs).
  6. Return the final value of open_tabs.

Time complexity: O(n), where n is the number of operations. Space complexity: O(1), as we only use a constant amount of space to store the open_tabs variable.

Optimized Solutions

Here are optimized solutions in various programming languages:

Java

View on GeeksforGeeks
public int countOpenTabs(String[] operations) {
    int open_tabs = 0;
    for (String operation : operations) {
        if (operation.equals("Open")) {
            open_tabs++;
        } else if (open_tabs > 0 && operation.equals("Close")) {
            open_tabs--;
        }
    }
    return open_tabs;
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.