首页 > 代码库 > 变位词排序
变位词排序
题目描述
请编写一个方法,对一个字符串数组进行排序,将所有变位词合并,保留其字典序最小的一个串。这里的变位词指变换其字母顺序所构成的新的词或短语。例如"triangle"和"integral"就是变位词。
给定一个string的数组str和数组大小int n,请返回排序合并后的数组。保证字符串串长小于等于20,数组大小小于等于300。
测试样例:
["ab","ba","abc","cba"]
返回:["ab","abc"]
class SortString { public: vector<string> sortStrings(vector<string> str, int n) { // write code here vector<string> res; sort(str.begin(),str.end()); set<string> set; for(const string &s :str){ string tmp(s); sort(tmp.begin(),tmp.end()); if(set.count(tmp)==0){ set.insert(tmp); res.push_back(s); } } return res; } };
变位词排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。