力扣1233.删除子文件夹
-
将folder按照字典序排序
- 遍历字符串数组
- 如果字符串长度 比ans中末尾的字符串长度还小
- 说明他肯定是新的文件夹
- 如果前m个字符完全相同,并且floder[i][m] == ‘/’
- 说明是子文件夹
-
class Solution {public:vector<string> removeSubfolders(vector<string>& folder) {sort(folder.begin(),folder.end());vector<string> ans = {folder[0]};for(int i=1;i<folder.size();i++){int m = ans.back().size();int n = folder[i].size();//folder[i][m] == '/' 防止出现"/a",“/ab"的情况if(m >= n || !(ans.back() == folder[i].substr(0,m) && folder[i][m] == '/'))ans.emplace_back(folder[i]);}return ans;}};