返回介绍

solution / 1900-1999 / 1974.Minimum Time to Type Word Using Special Typewriter / README_EN

发布于 2024-06-17 01:03:12 字数 4631 浏览 0 评论 0 收藏 0

1974. Minimum Time to Type Word Using Special Typewriter

中文文档

Description

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.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def minTimeToType(self, word: str) -> int:
    ans = prev = 0
    for c in word:
      curr = ord(c) - ord('a')
      t = abs(prev - curr)
      t = min(t, 26 - t)
      ans += t + 1
      prev = curr
    return ans
class Solution {
  public int minTimeToType(String word) {
    int ans = 0;
    int prev = 0;
    for (char c : word.toCharArray()) {
      int curr = c - 'a';
      int t = Math.abs(prev - curr);
      t = Math.min(t, 26 - t);
      ans += t + 1;
      prev = curr;
    }
    return ans;
  }
}
class Solution {
public:
  int minTimeToType(string word) {
    int ans = 0;
    int prev = 0;
    for (char& c : word) {
      int curr = c - 'a';
      int t = abs(prev - curr);
      t = min(t, 26 - t);
      ans += t + 1;
      prev = curr;
    }
    return ans;
  }
};
func minTimeToType(word string) int {
  ans, prev := 0, 0
  for _, c := range word {
    curr := int(c - 'a')
    t := abs(prev - curr)
    t = min(t, 26-t)
    ans += t + 1
    prev = curr
  }
  return ans
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文