返回介绍

solution / 2000-2099 / 2027.Minimum Moves to Convert String / README_EN

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

2027. Minimum Moves to Convert String

中文文档

Description

You are given a string s consisting of n characters which are either 'X' or 'O'.

A move is defined as selecting three consecutive characters of s and converting them to 'O'. Note that if a move is applied to the character 'O', it will stay the same.

Return _the minimum number of moves required so that all the characters of _s_ are converted to _'O'.

 

Example 1:

Input: s = "XXX"
Output: 1
Explanation: XXX -> OOO
We select all the 3 characters and convert them in one move.

Example 2:

Input: s = "XXOX"
Output: 2
Explanation: XXOX -> OOOX -> OOOO
We select the first 3 characters in the first move, and convert them to 'O'.
Then we select the last 3 characters and convert them so that the final string contains all 'O's.

Example 3:

Input: s = "OOOO"
Output: 0
Explanation: There are no 'X's in s to convert.

 

Constraints:

  • 3 <= s.length <= 1000
  • s[i] is either 'X' or 'O'.

Solutions

Solution 1: Greedy Algorithm

Traverse the string $s$. Whenever you encounter 'X', move the pointer $i$ three steps forward and add $1$ to the answer; otherwise, move the pointer $i$ one step forward.

The time complexity is $O(n)$, where $n$ represents the length of the string $s$.

class Solution:
  def minimumMoves(self, s: str) -> int:
    ans = i = 0
    while i < len(s):
      if s[i] == "X":
        ans += 1
        i += 3
      else:
        i += 1
    return ans
class Solution {
  public int minimumMoves(String s) {
    int ans = 0;
    for (int i = 0; i < s.length(); ++i) {
      if (s.charAt(i) == 'X') {
        ++ans;
        i += 2;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minimumMoves(string s) {
    int ans = 0;
    for (int i = 0; i < s.size(); ++i) {
      if (s[i] == 'X') {
        ++ans;
        i += 2;
      }
    }
    return ans;
  }
};
func minimumMoves(s string) (ans int) {
  for i := 0; i < len(s); i++ {
    if s[i] == 'X' {
      ans++
      i += 2
    }
  }
  return
}
function minimumMoves(s: string): number {
  const n = s.length;
  let ans = 0;
  let i = 0;
  while (i < n) {
    if (s[i] === 'X') {
      ans++;
      i += 3;
    } else {
      i++;
    }
  }
  return ans;
}
impl Solution {
  pub fn minimum_moves(s: String) -> i32 {
    let s = s.as_bytes();
    let n = s.len();
    let mut ans = 0;
    let mut i = 0;
    while i < n {
      if s[i] == b'X' {
        ans += 1;
        i += 3;
      } else {
        i += 1;
      }
    }
    ans
  }
}
int minimumMoves(char* s) {
  int n = strlen(s);
  int ans = 0;
  int i = 0;
  while (i < n) {
    if (s[i] == 'X') {
      ans++;
      i += 3;
    } else {
      i++;
    }
  }
  return ans;
}

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

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

发布评论

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