题解 | #牛棚品种分类#
牛棚品种分类
https://www.nowcoder.com/practice/0b6068f804b9426aa737ea8606e8d5c3
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param strs string字符串一维数组 * @return string字符串一维数组 */ function groupAnagrams(strs) { // write code here let res = [] let copyStr = [...strs] while (copyStr.length > 0) { let string = "" let currStr = copyStr[0] string += currStr let sortStr = [...currStr].sort().join('') copyStr.forEach((str, index) => { if ([...str].sort().join('') === sortStr && str !== currStr) { string += ',' + str copyStr.splice(index, 1) } }) copyStr.splice(0, 1) res.push(string) } return res.sort() } module.exports = { groupAnagrams: groupAnagrams };