
在处理诸如域名列表等字符串数据时,我们经常需要根据它们的最长公共后缀子串进行分组。例如,给定一个域名列表 ["samsung.phone.com", "lg.phone.com", "phone.com", "camera.dsrl.nikon.com", "amd.gpu.com", "intel.cpu.com"],我们的目标是创建一个字典(或映射),其中键是作为组标识的最长公共后缀子串,值是匹配该后缀的原始字符串列表。
这里的“最长公共后缀子串”特指那些在原始列表中也作为独立项出现的、且能作为其他字符串的后缀的子串。例如:
最终输出的字典结构应如下所示:
{
"phone.com": ["lg.phone.com", "samsung.phone.com"],
"camera.dsrl.nikon.com": [],
"amd.gpu.com": [],
"intel.cpu.com": []
}注意,如果一个字符串本身没有更长的字符串以其作为后缀,它将作为键存在,但其值列表可能为空。
我们将通过一个JavaScript函数 filterBySubdomain 来实现这一分组逻辑。该函数接收一个字符串列表,并返回一个分组字典。
该算法分多个步骤进行,以确保准确地识别和分组字符串:
初始化字典: 创建一个空字典 dict,并将输入列表 domList 中的每个字符串作为键,初始化其值为一个空数组。这一步确保所有潜在的分组键都存在。
const dict = {}; // key: subdomain, value: list of domains
domList.forEach((el) => (dict[el] = []));识别直接子域名关系: 通过嵌套循环遍历 domList。对于每一对不同的字符串 domList[i] 和 domList[j],检查 domList[j] 是否以 domList[i] 作为其“子域名”部分。这里的“子域名”被定义为 domList[j] 中第一个点号 . 之后的部分。如果匹配,则将 domList[j] 添加到 dict[domList[i]] 的列表中。
for (let i = 0; i < domList.length; i++) {
for (let j = 0; j < domList.length; j++) {
if (i !== j) {
const subdomain = domList[j].substring(domList[j].indexOf(".") + 1);
if (subdomain === domList[i]) {
dict[domList[i]].push(domList[j]);
}
}
}
}例如,如果 domList[j] 是 "samsung.phone.com",domList[i] 是 "phone.com",那么 subdomain 将是 "phone.com",匹配成功,"samsung.phone.com" 会被添加到 dict["phone.com"] 中。
识别并收集所有已分组的域名: 遍历当前 dict,将所有作为值列表中的域名收集到一个 mergedDomainList 中。这个列表包含了所有被识别为某个键的“子域名”的字符串。
let mergedDomainList = [];
for (const [subdomain, domainList] of Object.entries(dict)) {
mergedDomainList = [...mergedDomainList, ...domainList];
}移除不作为分组键的项: 遍历 mergedDomainList。如果一个字符串 x 在 dict 中作为键存在,但其值列表 dict[x] 为空(即它没有其他字符串以它为直接子域名),那么它就不应该成为一个分组键。因此,将其从 dict 中删除。这一步用于清理那些虽然存在于原始列表但实际上没有扮演“分组中心”角色的字符串。
mergedDomainList.forEach((x) => {
if (dict[x] && dict[x].length === 0) delete dict[x];
});例如,如果 intel.cpu.com 没有其他字符串以它为子域名,且 cpu.com 存在并成功分组了 intel.cpu.com,那么 intel.cpu.com 就不应再作为一个空键存在。
精炼分组列表以确保最长后缀匹配: 这是最关键的一步,用于实现“最长公共后缀子串”的逻辑。 首先,获取当前 dict 中所有剩余的键(它们是最终的分组标识)。 然后,对于 dict 中的每一个键值对,筛选其值列表:只保留那些不是当前 dict 中任何其他键的字符串。这意味着如果 samsung.phone.com 已经是一个键,那么 hello.samsung.phone.com 应该被分组到 samsung.phone.com 下,而不是 phone.com 下。通过将 samsung.phone.com 从 phone.com 的值列表中移除,我们确保了更长的后缀匹配优先。
const toRemove = Object.keys(dict); // 最终作为分组键的列表
for (const [key, value] of Object.entries(dict)) {
dict[key] = value.filter(function (el) {
return !toRemove.includes(el); // 移除那些本身也是分组键的字符串
});
}/**
* 根据最长公共后缀子串(子域名)对字符串列表进行分组。
* @param {string[]} domList 包含域名和子域名的字符串列表。
* @returns {Object.<string, string[]>} 一个字典,键为子域名,值为对应的域名列表。
*/
function filterBySubdomain(domList) {
const dict = {}; // 键: 子域名, 值: 对应的域名列表
// 1. 初始化字典:将所有输入字符串作为潜在键,值为空列表
domList.forEach((el) => (dict[el] = []));
// 2. 识别直接子域名关系
// 遍历所有字符串,查找哪些字符串是另一个字符串的直接子域名
for (let i = 0; i < domList.length; i++) {
for (let j = 0; j < domList.length; j++) {
if (i !== j) {
// 提取 domList[j] 的子域名部分(第一个点号之后的部分)
const firstDotIndex = domList[j].indexOf(".");
if (firstDotIndex === -1) continue; // 如果没有点号,则不构成子域名关系
const subdomain = domList[j].substring(firstDotIndex + 1);
if (subdomain === domList[i]) {
// 如果 domList[i] 是 domList[j] 的子域名
dict[domList[i]].push(domList[j]);
}
}
}
}
// 3. 收集所有已被分组的域名
let mergedDomainList = [];
for (const [subdomain, domainList] of Object.entries(dict)) {
mergedDomainList = [...mergedDomainList, ...domainList];
}
// 4. 移除那些作为键但其值列表为空的项(即它们本身没有子域名)
mergedDomainList.forEach((x) => {
// 检查 x 是否存在于 dict 且其值列表为空
if (dict.hasOwnProperty(x) && dict[x].length === 0) {
delete dict[x];
}
});
// 5. 精炼分组列表,确保最长后缀匹配原则
// 获取当前所有有效的分组键(子域名)
const finalKeys = Object.keys(dict);
for (const [key, value] of Object.entries(dict)) {
// 过滤掉值列表中那些本身也是最终分组键的字符串
// 这确保了例如 "hello.samsung.phone.com" 会被分组到 "samsung.phone.com"
// 而不是更通用的 "phone.com"
dict[key] = value.filter(function (el) {
return !finalKeys.includes(el);
});
}
return dict;
}// 示例输入数据
const x1 = [
"samsung.phone.com",
"lg.phone.com",
"phone.com",
"camera.dsrl.nikon.com",
"amd.gpu.com",
"intel.cpu.com",
];
const x2 = [
"samsung.phone.com",
"lg.phone.com",
"phone.com",
"camera.dsrl.nikon.com",
"amd.gpu.com",
"intel.cpu.com",
"cpu.com", // 新增项
];
const x3 = [
"samsung.phone.com",
"lg.phone.com",
"phone.com",
"camera.dsrl.nikon.com",
"amd.gpu.com",
"intel.cpu.com",
"cpu.com",
"hello.samsung.phone.com", // 新增项
];
// 调用函数进行分组
const result1 = filterBySubdomain(x1);
const result2 = filterBySubdomain(x2);
const result3 = filterBySubdomain(x3);
// 打印结果
console.log("--- 示例 1 ---");
console.log(result1);
console.log("\n--- 示例 2 ---");
console.log(result2);
console.log("\n--- 示例 3 ---");
console.log(result3);--- 示例 1 ---
{
'phone.com': [ 'samsung.phone.com', 'lg.phone.com' ],
'camera.dsrl.nikon.com': [],
'amd.gpu.com': [],
'intel.cpu.com': []
}
--- 示例 2 ---
{
'phone.com': [ 'samsung.phone.com', 'lg.phone.com' ],
'camera.dsrl.nikon.com': [],
'amd.gpu.com': [],
'cpu.com': [ 'intel.cpu.com' ]
}
--- 示例 3 ---
{
'samsung.phone.com': [ 'hello.samsung.phone.com' ],
'phone.com': [ 'lg.phone.com' ],
'camera.dsrl.nikon.com': [],
'amd.gpu.com': [],
'cpu.com': [ 'intel.cpu.com' ]
}通过上述 filterBySubdomain 函数,我们能够有效地根据最长公共后缀子串对字符串进行分组,尤其适用于域名或类似结构的数据整理。该方法清晰地定义了分组规则,并通过多阶段处理确保了结果的准确性和一致性。
以上就是根据最长公共后缀子串对字符串进行分组的教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号