2788. 按分隔符拆分字符串

给你一个字符串数组 words 和一个字符 separator ,请你按 separator 拆分 words 中的每个字符串。

返回一个由拆分后的新字符串组成的字符串数组,不包括空字符串

注意

  • separator 用于决定拆分发生的位置,但它不包含在结果字符串中。

  • 拆分可能形成两个以上的字符串。

  • 结果字符串必须保持初始相同的先后顺序。

 示例 1:

输入:words = ["one.two.three","four.five","six"], separator = "."
输出:["one","two","three","four","five","six"]
解释:在本示例中,我们进行下述拆分:

"one.two.three" 拆分为 "one", "two", "three"
"four.five" 拆分为 "four", "five"
"six" 拆分为 "six" 

因此,结果数组为 ["one","two","three","four","five","six"] 。

示例 2:

输入:words = ["$easy$","$problem$"], separator = "$"
输出:["easy","problem"]
解释:在本示例中,我们进行下述拆分:

"$easy$" 拆分为 "easy"(不包括空字符串)
"$problem$" 拆分为 "problem"(不包括空字符串)

因此,结果数组为 ["easy","problem"] 。

示例 3:

输入:words = ["|||"], separator = "|"
输出:[]
解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。 

 提示:

  • 1 <= words.length <= 100

  • 1 <= words[i].length <= 20

  • words[i] 中的字符要么是小写英文字母,要么就是字符串 ".,|$#@" 中的字符(不包括引号)

  • separator 是字符串 ".,|$#@" 中的某个字符(不包括引号)

思路

  • split()方法获取拆分后的字符串数组(使用正则效率低下,时间复杂度肯定高)

但是这里有一个注意点,由于separator 取值在".,|$#@" 这些特殊符号之中,需要进行转义操作

  • 使用索引配合substring()获取拆分后的字符串

代码

  • split()方法获取拆分后的字符串数组

class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> result = new ArrayList<>();
        for (String word : words) {
			//String[] split = word.split(String.valueOf(separator)); 注意这里
            String[] split = word.split("\\" + separator);
            for (String s : split) {
                if (!"".equals(s)) {
                    result.add(s);
                }
            }
        }
        return result;
    }
}
  • 使用索引配合substring()获取拆分后的字符串

class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> result = new ArrayList<>();
        for (String word : words) {
            int temp = 0;
            for (int i = 0; i < word.length(); i++) {
                if (word.charAt(i) == separator) {
                    if (i > temp) {
                        result.add(word.substring(temp, i));
                    }
                    temp = i + 1;
                }
            }
            if (word.length() > temp) {
                result.add(word.substring(temp));
            }
        }
        return result;
    }
}

当然,为了更快可以优化双for循环,将内循环抽取出来

class Solution {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> result = new ArrayList<>();
        for (String word : words) {
            computer(separator, word, result);
        }
        return result;
    }

    private void computer(char separator,String word, List<String> result){
        int temp = 0;
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == separator) {
                if (i > temp) {
                    result.add(word.substring(temp, i));
                }
                temp = i + 1;
            }
        }
        if (word.length() > temp) {
            result.add(word.substring(temp));
        }
    }
}