< Summary

Information
Class: LeetCode.Algorithms.KeyboardRow.KeyboardRowLookup
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KeyboardRow\KeyboardRowLookup.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 73
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
FindWords(...)100%44100%
IsSingleRow(...)100%44100%
GetRow(...)100%11100%
GetCharIndex(...)100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\KeyboardRow\KeyboardRowLookup.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.KeyboardRow;
 13
 14/// <inheritdoc />
 15public sealed class KeyboardRowLookup : IKeyboardRow
 16{
 117    private static readonly byte[] CharIndexToRow =
 118    [
 119        1, 2, 2, 1, 0, 1, 1, 1, 0, 1, 1, 1, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 0, 2
 120    ];
 21
 22    /// <summary>
 23    ///     Time complexity - O(n)
 24    ///     Space complexity - O(1)
 25    /// </summary>
 26    /// <param name="words"></param>
 27    /// <returns></returns>
 28    public string[] FindWords(string[] words)
 329    {
 330        var result = new List<string>(words.Length);
 31
 2332        foreach (var word in words)
 733        {
 734            if (IsSingleRow(word))
 435            {
 436                result.Add(word);
 437            }
 738        }
 39
 340        return result.ToArray();
 341    }
 42
 43    private static bool IsSingleRow(string word)
 744    {
 745        var firstRow = GetRow(word[0]);
 46
 4247        for (var i = 1; i < word.Length; i++)
 1748        {
 1749            var c = word[i];
 50
 1751            if (GetRow(c) == firstRow)
 1452            {
 1453                continue;
 54            }
 55
 356            return false;
 57        }
 58
 459        return true;
 760    }
 61
 62    private static int GetRow(char c)
 2463    {
 2464        var charIndex = GetCharIndex(c);
 65
 2466        return CharIndexToRow[charIndex];
 2467    }
 68
 69    private static int GetCharIndex(char c)
 2470    {
 2471        return (c | 32) - 'a';
 2472    }
 73}