1974. Minimum Time to Type Word Using Special Typewriter LeetCode Solution

In this guide, you will get 1974. Minimum Time to Type Word Using Special Typewriter LeetCode Solution with the best time and space complexity. The solution to Minimum Time to Type Word Using Special Typewriter problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Time to Type Word Using Special Typewriter solution in C++
  4. Minimum Time to Type Word Using Special Typewriter solution in Java
  5. Minimum Time to Type Word Using Special Typewriter solution in Python
  6. Additional Resources
1974. Minimum Time to Type Word Using Special Typewriter LeetCode Solution image

Problem Statement of Minimum Time to Type Word Using Special Typewriter

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.

See also  3244. Shortest Distance After Road Addition Queries II LeetCode Solution

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.

Constraints:

1 <= word.length <= 100
word consists of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1974. Minimum Time to Type Word Using Special Typewriter LeetCode Solution in C++

class Solution {
 public:
  int minTimeToType(string word) {
    int moves = 0;
    char letter = 'a';

    for (const char c : word) {
      const int diff = abs(c - letter);
      moves += min(diff, 26 - diff);
      letter = c;
    }

    return moves + word.length();
  }
};
/* code provided by PROGIEZ */

1974. Minimum Time to Type Word Using Special Typewriter LeetCode Solution in Java

class Solution {
  public int minTimeToType(String word) {
    int moves = 0;
    char letter = 'a';

    for (final char c : word.toCharArray()) {
      final int diff = Math.abs(c - letter);
      moves += Math.min(diff, 26 - diff);
      letter = c;
    }

    return moves + word.length();
  }
}
// code provided by PROGIEZ

1974. Minimum Time to Type Word Using Special Typewriter LeetCode Solution in Python

class Solution:
  def minTimeToType(self, word: str) -> int:
    moves = 0
    letter = 'a'

    for c in word:
      diff = abs(ord(c) - ord(letter))
      moves += min(diff, 26 - diff)
      letter = c

    return moves + len(word)
# code by PROGIEZ

Additional Resources

See also  412. Fizz Buzz LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.