2788. Split Strings by Separator
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { vector<string> parse(string& s, char ch) { vector<string> res; string now = ""; for(auto c : s) { if(c == ch) { if(now.length()) res.push_back(now); now = ""; } else now.push_back(c); } if(now.length()) res.push_back(now); return res; } public: vector<string> splitWordsBySeparator(vector<string>& words, char separator) { vector<string> res; for(auto w : words) { auto now = parse(w,separator); for(auto n : now) res.push_back(n); } return res; } };
|