2 minute read

There is a special typewriter with lowercase English letters ‘a’ to ‘z’ arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character ‘a’.

Each second, you may perform one of the following operations:

Move the pointer one character counterclockwise or clockwise. Type the character the pointer is currently on. Given a string word, return the minimum number of seconds to type out the characters in word.

Example 1:

Input: word = “abc” Output: 5 Explanation: The characters are printed as follows:

  • Type the character ‘a’ in 1 second since the pointer is initially on ‘a’.
  • Move the pointer clockwise to ‘b’ in 1 second.
  • Type the character ‘b’ in 1 second.
  • Move the pointer clockwise to ‘c’ in 1 second.
  • Type the character ‘c’ in 1 second. Example 2:

Input: word = “bza” Output: 7 Explanation: The characters are printed as follows:

  • Move the pointer clockwise to ‘b’ in 1 second.
  • Type the character ‘b’ in 1 second.
  • Move the pointer counterclockwise to ‘z’ in 2 seconds.
  • Type the character ‘z’ in 1 second.
  • Move the pointer clockwise to ‘a’ in 1 second.
  • Type the character ‘a’ in 1 second. Example 3:

Input: word = “zjpc” Output: 34 Explanation: The characters are printed as follows:

  • Move the pointer counterclockwise to ‘z’ in 1 second.
  • Type the character ‘z’ in 1 second.
  • Move the pointer clockwise to ‘j’ in 10 seconds.
  • Type the character ‘j’ in 1 second.
  • Move the pointer clockwise to ‘p’ in 6 seconds.
  • Type the character ‘p’ in 1 second.
  • Move the pointer counterclockwise to ‘c’ in 13 seconds.
  • Type the character ‘c’ in 1 second.
/**
 * @param {string} word
 * @return {number}
 */
var minTimeToType = function (word) {
  // Initialize a variable 'time' to keep track of the total time taken.
  let time = 0;

  // Initialize a variable 'lastChar' to store the last character processed, starting with 'a'.
  let lastChar = "a";

  // Iterate through each character 'w' in the 'word' array.
  for (const w of word) {
    // Calculate the clockwise distance between the current character and the last character.
    const clockwise = Math.abs(lastChar.charCodeAt() - w.charCodeAt());

    // Calculate the counter-clockwise distance by subtracting the clockwise distance from 26 (the number of letters in the alphabet).
    const counterClockWise = Math.abs(clockwise - 26);

    // Determine the minimum distance between clockwise and counter-clockwise, representing the shortest turn.
    const turn = Math.min(clockwise, counterClockWise);

    // Update the total time by adding the turn distance plus 1.
    time += turn + 1;

    // Update 'lastChar' to the current character for the next iteration.
    lastChar = w;
  }

  // Return the total time taken to traverse the word.
  return time;
};

The time complexity of the loop is O(n), where n is the length of the input string s. The loop iterates through each character in the ‘word’ array exactly once.

Regardless of the length of the ‘word’ array, the amount of additional memory required remains constant, making the space complexity O(1).

Leave a comment