DEV Community

Cover image for Leet Code — 2194. Cells in a Range on an Excel Sheet
Ben Pereira
Ben Pereira

Posted on

Leet Code — 2194. Cells in a Range on an Excel Sheet

It’s an easy problem by Leet Code, the description being:

A cell (r, c) of an excel sheet is represented as a string "

" where: denotes the column number c of the cell. It is represented by alphabetical letters.

For example, the 1st column is denoted by 'A', the 2nd by 'B', the 3rd by 'C', and so on.

is the row number r of the cell. The rth row is represented by the integer r.

You are given a string s in the format ":", where represents the column c1, represents the row r1, represents the column c2, and represents the row r2, such that r1 <= r2 and c1 <= c2.

Return the list of cells (x, y) such that r1 <= x <= r2 and c1 <= y <= c2. The cells should be represented as strings in the format mentioned above and be sorted in non-decreasing order first by columns and then by rows.

Constraints:

s.length == 5
'A' <= s[0] <= s[3] <= 'Z'
'1' <= s[1] <= s[4] <= '9'

s consists of uppercase English letters, digits and ':'.

Is much easier to understand by checking the second example:

Image description

Input: s = “A1:F1”
Output: [“A1”,”B1",”C1",”D1",”E1",”F1"]

Explanation:

The above diagram shows the cells which should be present in the list.

The red arrow denotes the order in which the cells should be presented.

You have two cells separated by colon and the first character is the column and second one is the row.

The process to get this done is first separating the first cell from the second, then retrieving the row number from both and the column character.

You can have a string that would help you follow through the characters and then you can iterate it and fill up all the items requested from the list in the response.

class Solution {

    final String cols = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public List<String> cellsInRange(String s) {

        // split from and to col/row
        final String[] splittedS = s.split(":");

        final String from = splittedS[0];
        final String to = splittedS[1];

        // get columns
        final char fromCol = from.charAt(0);
        final char toCol = to.charAt(0);

        // get columns indexes
        final int fromColIndex = cols.indexOf(fromCol);
        final int toColIndex = cols.indexOf(toCol);

        // get rows
        // can't use integer parse int due to be char and not string
        final int fromRow = Character.getNumericValue(from.charAt(1)); 
        final int toRow = Character.getNumericValue(to.charAt(1));

        List<String> response = new ArrayList<>();

        // using the info gathered to iterate on a O(n)2
        for(int i=fromColIndex;i<=toColIndex;i++){
            for(int j=fromRow;j<=toRow;j++){
                response.add(cols.charAt(i) + String.valueOf(j));
            }

        }

        return response;
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 7 ms, faster than 20.12% of Java online submissions for Cells in a Range on an Excel Sheet.

Memory Usage: 44 MB, less than 40.14% of Java online submissions for Cells in a Range on an Excel Sheet.

This works fine but there is better way to do it, since characters can jump from one to another you could add it straight away on the iteration and that will also work:

class Solution {

    public List<String> cellsInRange(String s) {

        // get columns
        final char fromCol = s.charAt(0);
        final char toCol = s.charAt(3);

        // get rows
        final char fromRow = s.charAt(1); 
        final char toRow = s.charAt(4);

        List<String> response = new ArrayList<>();

        // using the info gathered to iterate on a O(n)2
        for(char i=fromCol ; i<=toCol ; ++i){
            for(char j=fromRow ; j<=toRow ;++j){
                response.add(String.valueOf(i)+String.valueOf(j));
            }

        }

        return response;
    }
}
Enter fullscreen mode Exit fullscreen mode

Runtime: 2 ms, faster than 68.90% of Java online submissions for Cells in a Range on an Excel Sheet.

Memory Usage: 43.6 MB, less than 95.53% of Java online submissions for Cells in a Range on an Excel Sheet.

Is much faster because you don’t need to split or convert anything, just grab the characters and iterate it as needed.

We usually tend to forget about the power of characters and what can be done with it and just focus on numbers and things that are more human side and less machine side, so is a good one to keep in mind that this is possible as well.


That’s it! If there is anything thing else to discuss feel free to drop a comment, until next post! :)

Top comments (0)