返回介绍

solution / 2900-2999 / 2957.Remove Adjacent Almost-Equal Characters / README_EN

发布于 2024-06-17 01:02:58 字数 4874 浏览 0 评论 0 收藏 0

2957. Remove Adjacent Almost-Equal Characters

中文文档

Description

You are given a 0-indexed string word.

In one operation, you can pick any index i of word and change word[i] to any lowercase English letter.

Return _the minimum number of operations needed to remove all adjacent almost-equal characters from_ word.

Two characters a and b are almost-equal if a == b or a and b are adjacent in the alphabet.

 

Example 1:

Input: word = "aaaaa"
Output: 2
Explanation: We can change word into "acaca" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.

Example 2:

Input: word = "abddez"
Output: 2
Explanation: We can change word into "ybdoez" which does not have any adjacent almost-equal characters.
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 2.

Example 3:

Input: word = "zyxyxyz"
Output: 3
Explanation: We can change word into "zaxaxaz" which does not have any adjacent almost-equal characters. 
It can be shown that the minimum number of operations needed to remove all adjacent almost-equal characters from word is 3.

 

Constraints:

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

Solutions

Solution 1: Greedy

We start traversing the string word from index $1$. If word[i] and word[i - 1] are approximately equal, we greedily replace word[i] with a character that is not equal to both word[i - 1] and word[i + 1] (we can choose not to perform the replacement operation, just record the number of operations). Then, we skip word[i + 1] and continue to traverse the string word.

Finally, we return the recorded number of operations.

The time complexity is $O(n)$, where $n$ is the length of the string word. The space complexity is $O(1)$.

class Solution:
  def removeAlmostEqualCharacters(self, word: str) -> int:
    ans = 0
    i, n = 1, len(word)
    while i < n:
      if abs(ord(word[i]) - ord(word[i - 1])) < 2:
        ans += 1
        i += 2
      else:
        i += 1
    return ans
class Solution {
  public int removeAlmostEqualCharacters(String word) {
    int ans = 0, n = word.length();
    for (int i = 1; i < n; ++i) {
      if (Math.abs(word.charAt(i) - word.charAt(i - 1)) < 2) {
        ++ans;
        ++i;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int removeAlmostEqualCharacters(string word) {
    int ans = 0, n = word.size();
    for (int i = 1; i < n; ++i) {
      if (abs(word[i] - word[i - 1]) < 2) {
        ++ans;
        ++i;
      }
    }
    return ans;
  }
};
func removeAlmostEqualCharacters(word string) (ans int) {
  for i := 1; i < len(word); i++ {
    if abs(int(word[i])-int(word[i-1])) < 2 {
      ans++
      i++
    }
  }
  return
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function removeAlmostEqualCharacters(word: string): number {
  let ans = 0;
  for (let i = 1; i < word.length; ++i) {
    if (Math.abs(word.charCodeAt(i) - word.charCodeAt(i - 1)) < 2) {
      ++ans;
      ++i;
    }
  }
  return ans;
}

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

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

发布评论

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