LeetCode 2194: Cells in a Range on an Excel Sheet Solution

Master LeetCode problem 2194 (Cells in a Range on an Excel Sheet), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2194. Cells in a Range on an Excel Sheet

Easy

Problem Explanation

Explanation:

  • Parse the input string s to extract the starting and ending columns and rows.
  • Iterate over all cells in the given range and store them in a list.
  • Sort the list of cells based on columns and then rows.
  • Return the sorted list of cells.

Time Complexity: O((r2-r1) * (c2-c1)) where r1, r2, c1, c2 are the starting and ending rows and columns.
Space Complexity: O((r2-r1) * (c2-c1))

:

Solution Code

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Solution {
    public List<String> cellsInRange(String s) {
        char startCol = s.charAt(0);
        int startRow = s.charAt(1) - '0';
        char endCol = s.charAt(3);
        int endRow = s.charAt(4) - '0';

        List<String> cells = new ArrayList<>();
        for (char col = startCol; col <= endCol; col++) {
            for (int row = startRow; row <= endRow; row++) {
                cells.add(col + "" + row);
            }
        }

        Collections.sort(cells);
        return cells;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2194 (Cells in a Range on an Excel Sheet)?

This page provides optimized solutions for LeetCode problem 2194 (Cells in a Range on an Excel Sheet) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2194 (Cells in a Range on an Excel Sheet)?

The time complexity for LeetCode 2194 (Cells in a Range on an Excel Sheet) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2194 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2194 in Java, C++, or Python.

Back to LeetCode Solutions