> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Single-Row Keyboard Python Solution with Tests

> Tested Python solution for LeetCode 1165 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 1165, [Easy](/catalog/easy). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/single-row-keyboard/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1165   # by problem number
lcpy gen -s single_row_keyboard   # by problem name
```

## Problem

There is a special keyboard with **all keys in a single row**.

Given a string `keyboard` of length `26` indicating the layout of the keyboard (indexed from `0` to `25`). Initially, your finger is at index `0`. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index `i` to index `j` is `|i - j|`.

You want to type a string `word`. Write a function to calculate how much time it takes to type it with one finger.

### Examples

```
Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
Output: 4
Explanation: The index moves from 0 to 2 to write 'c' then to 1 to write 'b' then to 0 again to write 'a'.
Total time = 2 + 1 + 1 = 4.
```

```
Input: keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
Output: 73
```

### Constraints

* keyboard.length == 26
* keyboard contains each English lowercase letter exactly once in some order.
* 1 \<= word.length \<= 10^4
* word\[i] is an English lowercase letter.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/single_row_keyboard/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/single_row_keyboard/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + 26)
    # Space: O(26)
    def calculate_time(self, keyboard: str, word: str) -> int:
        pos = {char: i for i, char in enumerate(keyboard)}
        total = current = 0
        for char in word:
            target = pos[char]
            total += abs(target - current)
            current = target
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n + 26) | O(26) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.