返回介绍

solution / 1500-1599 / 1540.Can Convert String in K Moves / README_EN

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

1540. Can Convert String in K Moves

中文文档

Description

Given two strings s and t, your goal is to convert s into t in k moves or less.

During the ith (1 <= i <= kmove you can:

  • Choose any index j (1-indexed) from s, such that 1 <= j <= s.length and j has not been chosen in any previous move, and shift the character at that index i times.
  • Do nothing.

Shifting a character means replacing it by the next letter in the alphabet (wrapping around so that 'z' becomes 'a'). Shifting a character by i means applying the shift operations i times.

Remember that any index j can be picked at most once.

Return true if it's possible to convert s into t in no more than k moves, otherwise return false.

 

Example 1:

Input: s = "input", t = "ouput", k = 9
Output: true
Explanation: In the 6th move, we shift 'i' 6 times to get 'o'. And in the 7th move we shift 'n' to get 'u'.

Example 2:

Input: s = "abc", t = "bcd", k = 10
Output: false
Explanation: We need to shift each character in s one time to convert it into t. We can shift 'a' to 'b' during the 1st move. However, there is no way to shift the other characters in the remaining moves to obtain t from s.

Example 3:

Input: s = "aab", t = "bbb", k = 27
Output: true
Explanation: In the 1st move, we shift the first 'a' 1 time to get 'b'. In the 27th move, we shift the second 'a' 27 times to get 'b'.

 

Constraints:

  • 1 <= s.length, t.length <= 10^5
  • 0 <= k <= 10^9
  • s, t contain only lowercase English letters.

Solutions

Solution 1

class Solution:
  def canConvertString(self, s: str, t: str, k: int) -> bool:
    if len(s) != len(t):
      return False
    cnt = [0] * 26
    for a, b in zip(s, t):
      x = (ord(b) - ord(a) + 26) % 26
      cnt[x] += 1
    for i in range(1, 26):
      if i + 26 * (cnt[i] - 1) > k:
        return False
    return True
class Solution {
  public boolean canConvertString(String s, String t, int k) {
    if (s.length() != t.length()) {
      return false;
    }
    int[] cnt = new int[26];
    for (int i = 0; i < s.length(); ++i) {
      int x = (t.charAt(i) - s.charAt(i) + 26) % 26;
      ++cnt[x];
    }
    for (int i = 1; i < 26; ++i) {
      if (i + 26 * (cnt[i] - 1) > k) {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool canConvertString(string s, string t, int k) {
    if (s.size() != t.size()) {
      return false;
    }
    int cnt[26]{};
    for (int i = 0; i < s.size(); ++i) {
      int x = (t[i] - s[i] + 26) % 26;
      ++cnt[x];
    }
    for (int i = 1; i < 26; ++i) {
      if (i + 26 * (cnt[i] - 1) > k) {
        return false;
      }
    }
    return true;
  }
};
func canConvertString(s string, t string, k int) bool {
  if len(s) != len(t) {
    return false
  }
  cnt := [26]int{}
  for i := range s {
    x := (t[i] - s[i] + 26) % 26
    cnt[x]++
  }
  for i := 1; i < 26; i++ {
    if i+26*(cnt[i]-1) > k {
      return false
    }
  }
  return true
}

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

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

发布评论

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