bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1165. Single-Row Keyboard

1165. Single-Row Keyboard

Leetcode Solutions

Storing Indexes for Quick Lookup

  1. Create an array keyIndices of size 26 to store the index of each letter in the keyboard.
  2. Iterate over the keyboard string and store the index of each character in keyIndices.
  3. Initialize an integer result to 0 to store the total time.
  4. Initialize an integer prev to 0 to represent the initial finger position.
  5. Iterate over each character c in the word string.
  6. For each character c, find its index in keyIndices and calculate the absolute difference with prev.
  7. Add the absolute difference to result.
  8. Update prev to the index of c.
  9. Return result as the total time required to type the word.
UML Thumbnail

Brute Force Character Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...