Date: 2026-06-06
Time: 19:07
This file solves LeetCode 1165 - Single Row Keyboard. It calculates the total time to type a word on a keyboard where all 26 letters are arranged in a single row. The "time" to move between keys equals the absolute difference of their positions. The finger starts at position 0 (the first key).
calculate_time(keyboard: str, word: str) -> intThe sole public function. Contract:
keyboard is a 26-character string representing the physical layout (each character's index is its position on the keyboard). word is the string to type.word exists in keyboard. No validation is performed.TestCalculateTimeSeven unit tests covering:
Precomputed index map: The function builds a pos dictionary mapping each character to its keyboard index in O(26) time before iterating the word. This is the standard hash-map-for-O(1)-lookup idiom — avoids calling keyboard.index(c) (O(26)) per character.
Accumulator loop: Tracks current position and accumulates total cost in a single pass over word. This is the canonical "simulate the process" pattern for position-tracking problems.
Self-contained test file: Tests live alongside the solution rather than in a separate test directory, following the project-wide convention visible in the repository structure.
Imports: Only unittest from the standard library. No external dependencies.
Imported by: The "Imported By" list in the prompt is misleading — it lists hundreds of test files across the entire repo. This is likely an artifact of the analysis tool picking up import unittest as a shared dependency, not actual imports of this module's calculatetime function. The real consumer is single-row-keyboard/testsolution.py.
1. Build pos: {c: i for i, c in enumerate(keyboard)} — O(26) dictionary comprehension.
2. Initialize current = 0 (finger starts at the leftmost key).
3. For each character c in word:
abs(current - pos[c]) to total.current to pos[c].4. Return total.
Total complexity: O(n) where n = len(word), with O(1) space beyond the fixed-size 26-entry dictionary.
keyboard must be a permutation of 26 lowercase letters (not enforced, but assumed).word must appear in keyboard, otherwise pos[c] raises KeyError.current always reflects the position of the most recently typed character (or 0 before any typing).None. Invalid inputs (characters not in the keyboard, empty keyboard, non-lowercase characters) will raise unhandled KeyError or produce silently wrong results. This is typical for LeetCode solutions where inputs are guaranteed valid by the problem constraints.
single-row-keyboard/plan.md — The planning document may reveal alternative approaches considered (e.g., direct str.index vs. hash map)single-row-keyboard/review.md — Code review notes that may discuss complexity tradeoffs or edge casesminimum-time-to-type-word-using-special-typewriter/solution.py:minTimeToType — A closely related problem where the keyboard is circular (modular arithmetic instead of absolute difference)keyboard-row/solution.py — Another keyboard-position problem; compare how keyboard layout is modeledindex-map-pattern — The {char: index} precomputation pattern recurs across many string/array problems in this repo (e.g., decode-the-message, find-anagram-mappings)single-row-keyboard-linear-time — calculate_time runs in O(n) time and O(1) auxiliary space (the pos dict is fixed at 26 entries regardless of input size)single-row-keyboard-no-validation — The function assumes all characters in word exist in keyboard; a missing character raises an unhandled KeyErrorsingle-row-keyboard-start-position — The finger always starts at index 0 of the keyboard string, not at the position of any particular lettersingle-row-keyboard-self-contained — The file combines solution and tests in a single module, runnable via python -m unittest or python solution.py