返回介绍

solution / 2300-2399 / 2315.Count Asterisks / README_EN

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

2315. Count Asterisks

中文文档

Description

You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth.

Return _the number of _'*'_ in _s_, excluding the _'*'_ between each pair of _'|'.

Note that each '|' will belong to exactly one pair.

 

Example 1:

Input: s = "l|*e*et|c**o|*de|"
Output: 2
Explanation: The considered characters are underlined: "l|*e*et|c**o|*de|".
The characters between the first and second '|' are excluded from the answer.
Also, the characters between the third and fourth '|' are excluded from the answer.
There are 2 asterisks considered. Therefore, we return 2.

Example 2:

Input: s = "iamprogrammer"
Output: 0
Explanation: In this example, there are no asterisks in s. Therefore, we return 0.

Example 3:

Input: s = "yo|uar|e**|b|e***au|tifu|l"
Output: 5
Explanation: The considered characters are underlined: "yo|uar|e**|b|e***au|tifu|l". There are 5 asterisks considered. Therefore, we return 5.

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters, vertical bars '|', and asterisks '*'.
  • s contains an even number of vertical bars '|'.

Solutions

Solution 1

class Solution:
  def countAsterisks(self, s: str) -> int:
    ans, ok = 0, 1
    for c in s:
      if c == "*":
        ans += ok
      elif c == "|":
        ok ^= 1
    return ans
class Solution {
  public int countAsterisks(String s) {
    int ans = 0;
    for (int i = 0, ok = 1; i < s.length(); ++i) {
      char c = s.charAt(i);
      if (c == '*') {
        ans += ok;
      } else if (c == '|') {
        ok ^= 1;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countAsterisks(string s) {
    int ans = 0, ok = 1;
    for (char& c : s) {
      if (c == '*') {
        ans += ok;
      } else if (c == '|') {
        ok ^= 1;
      }
    }
    return ans;
  }
};
func countAsterisks(s string) (ans int) {
  ok := 1
  for _, c := range s {
    if c == '*' {
      ans += ok
    } else if c == '|' {
      ok ^= 1
    }
  }
  return
}
function countAsterisks(s: string): number {
  let ans = 0;
  let ok = 1;
  for (const c of s) {
    if (c === '*') {
      ans += ok;
    } else if (c === '|') {
      ok ^= 1;
    }
  }
  return ans;
}
impl Solution {
  pub fn count_asterisks(s: String) -> i32 {
    let mut ans = 0;
    let mut ok = 1;
    for &c in s.as_bytes() {
      if c == b'*' {
        ans += ok;
      } else if c == b'|' {
        ok ^= 1;
      }
    }
    ans
  }
}
public class Solution {
  public int CountAsterisks(string s) {
    int ans = 0, ok = 1;
    foreach (char c in s) {
      if (c == '*') {
        ans += ok;
      } else if (c == '|') {
        ok ^= 1;
      }
    }
    return ans;
  }
}
int countAsterisks(char* s) {
  int ans = 0;
  int ok = 1;
  for (int i = 0; s[i]; i++) {
    if (s[i] == '*') {
      ans += ok;
    } else if (s[i] == '|') {
      ok ^= 1;
    }
  }
  return ans;
}

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

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

发布评论

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