返回介绍

solution / 2700-2799 / 2788.Split Strings by Separator / README_EN

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

2788. Split Strings by Separator

中文文档

Description

Given an array of strings words and a character separator, split each string in words by separator.

Return _an array of strings containing the new strings formed after the splits, excluding empty strings._

Notes

  • separator is used to determine where the split should occur, but it is not included as part of the resulting strings.
  • A split may result in more than two strings.
  • The resulting strings must maintain the same order as they were initially given.

 

Example 1:

Input: words = ["one.two.three","four.five","six"], separator = "."
Output: ["one","two","three","four","five","six"]
Explanation: In this example we split as follows:

"one.two.three" splits into "one", "two", "three"
"four.five" splits into "four", "five"
"six" splits into "six" 

Hence, the resulting array is ["one","two","three","four","five","six"].

Example 2:

Input: words = ["$easy$","$problem$"], separator = "$"
Output: ["easy","problem"]
Explanation: In this example we split as follows: 

"$easy$" splits into "easy" (excluding empty strings)
"$problem$" splits into "problem" (excluding empty strings)

Hence, the resulting array is ["easy","problem"].

Example 3:

Input: words = ["|||"], separator = "|"
Output: []
Explanation: In this example the resulting split of "|||" will contain only empty strings, so we return an empty array []. 

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 20
  • characters in words[i] are either lowercase English letters or characters from the string ".,|$#@" (excluding the quotes)
  • separator is a character from the string ".,|$#@" (excluding the quotes)

Solutions

Solution 1: Simulation

We traverse the string array $words$. For each string $w$, we use separator as the delimiter to split it. If the split string is not empty, we add it to the answer array.

The time complexity is $O(n \times m)$, and the space complexity is $O(m)$, where $n$ is the length of the string array $words$, and $m$ is the maximum length of the strings in the array $words$.

class Solution:
  def splitWordsBySeparator(self, words: List[str], separator: str) -> List[str]:
    return [s for w in words for s in w.split(separator) if s]
import java.util.regex.Pattern;

class Solution {
  public List<String> splitWordsBySeparator(List<String> words, char separator) {
    List<String> ans = new ArrayList<>();
    for (var w : words) {
      for (var s : w.split(Pattern.quote(String.valueOf(separator)))) {
        if (s.length() > 0) {
          ans.add(s);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<string> splitWordsBySeparator(vector<string>& words, char separator) {
    vector<string> ans;
    for (const auto& w : words) {
      istringstream ss(w);
      string s;
      while (getline(ss, s, separator)) {
        if (!s.empty()) {
          ans.push_back(s);
        }
      }
    }
    return ans;
  }
};
func splitWordsBySeparator(words []string, separator byte) (ans []string) {
  for _, w := range words {
    for _, s := range strings.Split(w, string(separator)) {
      if s != "" {
        ans = append(ans, s)
      }
    }
  }
  return
}
function splitWordsBySeparator(words: string[], separator: string): string[] {
  return words.flatMap(w => w.split(separator).filter(s => s.length > 0));
}

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

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

发布评论

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