返回介绍

solution / 2500-2599 / 2516.Take K of Each Character From Left and Right / README_EN

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

2516. Take K of Each Character From Left and Right

中文文档

Description

You are given a string s consisting of the characters 'a', 'b', and 'c' and a non-negative integer k. Each minute, you may take either the leftmost character of s, or the rightmost character of s.

Return_ the minimum number of minutes needed for you to take at least _k_ of each character, or return _-1_ if it is not possible to take _k_ of each character._

 

Example 1:

Input: s = "aabaaaacaabc", k = 2
Output: 8
Explanation: 
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
A total of 3 + 5 = 8 minutes is needed.
It can be proven that 8 is the minimum number of minutes needed.

Example 2:

Input: s = "a", k = 1
Output: -1
Explanation: It is not possible to take one 'b' or 'c' so return -1.

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of only the letters 'a', 'b', and 'c'.
  • 0 <= k <= s.length

Solutions

Solution 1

class Solution:
  def takeCharacters(self, s: str, k: int) -> int:
    cnt = Counter(s)
    if any(cnt[c] < k for c in "abc"):
      return -1
    ans = j = 0
    for i, c in enumerate(s):
      cnt[c] -= 1
      while cnt[c] < k:
        cnt[s[j]] += 1
        j += 1
      ans = max(ans, i - j + 1)
    return len(s) - ans
class Solution {
  public int takeCharacters(String s, int k) {
    int[] cnt = new int[3];
    int n = s.length();
    for (int i = 0; i < n; ++i) {
      ++cnt[s.charAt(i) - 'a'];
    }
    if (cnt[0] < k || cnt[1] < k || cnt[2] < k) {
      return -1;
    }
    int ans = 0, j = 0;
    for (int i = 0; i < n; ++i) {
      int c = s.charAt(i) - 'a';
      --cnt[c];
      while (cnt[c] < k) {
        ++cnt[s.charAt(j++) - 'a'];
      }
      ans = Math.max(ans, i - j + 1);
    }
    return n - ans;
  }
}
class Solution {
public:
  int takeCharacters(string s, int k) {
    int cnt[3] = {0};
    for (char& c : s) ++cnt[c - 'a'];
    if (cnt[0] < k || cnt[1] < k || cnt[2] < k) return -1;
    int ans = 0, j = 0;
    int n = s.size();
    for (int i = 0; i < n; ++i) {
      int c = s[i] - 'a';
      --cnt[c];
      while (cnt[c] < k) {
        ++cnt[s[j++] - 'a'];
      }
      ans = max(ans, i - j + 1);
    }
    return n - ans;
  }
};
func takeCharacters(s string, k int) int {
  cnt := [3]int{}
  for _, c := range s {
    cnt[c-'a']++
  }
  if cnt[0] < k || cnt[1] < k || cnt[2] < k {
    return -1
  }
  ans, j := 0, 0
  for i, c := range s {
    c -= 'a'
    cnt[c]--
    for cnt[c] < k {
      cnt[s[j]-'a']++
      j++
    }
    ans = max(ans, i-j+1)
  }
  return len(s) - ans
}
function takeCharacters(s: string, k: number): number {
  const getIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
  const count = [0, 0, 0];
  for (const c of s) {
    count[getIndex(c)]++;
  }
  if (count.some(v => v < k)) {
    return -1;
  }
  const n = s.length;
  let ans = 0;
  for (let i = 0, j = 0; j < n; j++) {
    count[getIndex(s[j])]--;
    while (count[getIndex(s[j])] < k) {
      count[getIndex(s[i])]++;
      i++;
    }
    ans = Math.max(ans, j - i + 1);
  }
  return n - ans;
}
impl Solution {
  pub fn take_characters(s: String, k: i32) -> i32 {
    let s = s.as_bytes();
    let mut count = vec![0; 3];
    for c in s.iter() {
      count[(c - b'a') as usize] += 1;
    }
    if count.iter().any(|v| *v < k) {
      return -1;
    }
    let n = s.len();
    let mut ans = 0;
    let mut i = 0;
    for j in 0..n {
      count[(s[j] - b'a') as usize] -= 1;
      while count[(s[j] - b'a') as usize] < k {
        count[(s[i] - b'a') as usize] += 1;
        i += 1;
      }
      ans = ans.max(j - i + 1);
    }
    (n - ans) as i32
  }
}

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

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

发布评论

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