返回介绍

solution / 1100-1199 / 1119.Remove Vowels from a String / README_EN

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

1119. Remove Vowels from a String

中文文档

Description

Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return the new string.

 

Example 1:

Input: s = "leetcodeisacommunityforcoders"
Output: "ltcdscmmntyfrcdrs"

Example 2:

Input: s = "aeiou"
Output: ""

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of only lowercase English letters.

Solutions

Solution 1: Simulation

We can directly traverse the string according to the requirements of the problem, and append characters that are not vowels to the result string.

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

class Solution:
  def removeVowels(self, s: str) -> str:
    return "".join(c for c in s if c not in "aeiou")
class Solution {
  public String removeVowels(String s) {
    StringBuilder ans = new StringBuilder();
    for (int i = 0; i < s.length(); ++i) {
      char c = s.charAt(i);
      if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
        ans.append(c);
      }
    }
    return ans.toString();
  }
}
class Solution {
public:
  string removeVowels(string s) {
    string ans;
    for (char& c : s) {
      if (!(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')) {
        ans += c;
      }
    }
    return ans;
  }
};
func removeVowels(s string) string {
  ans := []rune{}
  for _, c := range s {
    if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
      ans = append(ans, c)
    }
  }
  return string(ans)
}
function removeVowels(s: string): string {
  return s.replace(/[aeiou]/g, '');
}

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

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

发布评论

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