当前位置: 首页 > news >正文

详细介绍:LeetCode //C - 893. Groups of Special-Equivalent Strings

893. Groups of Special-Equivalent Strings

You are given an array of strings of the same length words.

In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i].

Two strings words[i] and words[j] are special-equivalent if after any number of moves, words[i] == words[j].

  • For example, words[i] = “zzxy” and words[j] = “xyzz” are special-equivalent because we may make the moves “zzxy” -> “xzzy” -> “xyzz”.

A group of special-equivalent strings from words is a non-empty subset of words such that:

  • Every pair of strings in the group are special equivalent, and
  • The group is the largest size possible (i.e., there is not a string words[i] not in the group such that words[i] is special-equivalent to every string in the group).

Returnthe number of groups of special-equivalent strings from words.

Example 1:

Input:words = [“abcd”,“cdab”,“cbad”,“xyzz”,“zzxy”,“zzyx”]
Output: 3
**Explanation: **
One group is [“abcd”, “cdab”, “cbad”], since they are all pairwise special equivalent, and none of the other strings is all pairwise special equivalent to these.
The other two groups are [“xyzz”, “zzxy”] and [“zzyx”].
Note that in particular, “zzxy” is not special equivalent to “zzyx”.

Example 2:

Input:words = [“abc”,“acb”,“bac”,“bca”,“cab”,“cba”]
Output: 3

Constraints:

From: LeetCode
Link: 893. Groups of Special-Equivalent Strings


Solution:

Ideas:
Code:
static int equal52(const int *a, const int *b) {
for (int i = 0; i < 52; ++i) if (a[i] != b[i]) return 0;
return 1;
}
int numSpecialEquivGroups(char** words, int wordsSize) {
if (wordsSize <= 0) return 0;
// Store unique 52-int signatures (26 counts for even idx, 26 for odd idx)
int *sigs = (int *)malloc(wordsSize * 52 * sizeof(int));
int uniq = 0;
for (int i = 0; i < wordsSize; ++i) {
int sig[52] = {0};
// Build signature: counts by parity
for (int k = 0; words[i][k] != '\0'; ++k) {
int c = words[i][k] - 'a';
if ((k & 1) == 0) sig[c]++;         // even positions
else               sig[26 + c]++;   // odd positions
}
// Check if this signature already exists
int found = 0;
for (int j = 0; j < uniq; ++j) {
if (equal52(sig, sigs + j * 52)) {
found = 1;
break;
}
}
if (!found) {
memcpy(sigs + uniq * 52, sig, 52 * sizeof(int));
++uniq;
}
}
free(sigs);
return uniq;
}
http://www.jsqmd.com/news/41978/

相关文章:

  • 11 月 11 日
  • 2025年国内烘干技术厂家排行榜:十大优质供应商深度评测
  • 2025年烘干技术源头厂家推荐排行榜前十名
  • Docmost部署与应用实践
  • [论文笔记] Lifting On-Demand Analysis to Higher-Order Languages
  • 2025年烘干机厂家排行榜前十强推荐:行业精选与选择指南
  • Java 可变参数机制
  • 11 月 3 日
  • 002 vue3-admin项目的目录及文件说明之src目录及其子目录、子文件
  • 全国性搬家公司推荐榜:从运费优势、专业度、靠谱口碑、费用便宜划算等综合实力排名
  • C# 高级类型 Dictionary(学习笔记4)
  • Java 垃圾收集机制
  • Metasploit Framework 6.4.99 (macOS, Linux, Windows) - 开源渗透测试框架
  • 小程序获取OCR识别结果,示例代码
  • 20232405 2024-2025-1 《网络与系统攻防技术》实验五实验报告
  • Invicti v25.11 发布,新增功能简介
  • Acunetix v25.11 发布,新增功能简介
  • 【运维自动化-标准运维】变量的高级用法
  • MySQL数据过滤与计算字段实战技术指南
  • 2025-11-14 PQ v.Next日志记录
  • 详细介绍:K8s 安全机制全解析
  • 详细介绍:MySQL索引指南
  • 实用指南:【第五章:计算机视觉-项目实战之推荐/广告系统】1.推荐系统基础与召回算法-(6)召回算法之u2i: FM、deepFM、召回双塔原理精讲与实战
  • 微前端架构演进:决策、实践与协同
  • OI 技术动作 注意事项集合
  • Java 设计模式—— 责任链模式:从原理到 SpringBoot 最优搭建
  • 大模型基础补全计划(七)---Transformer(多头注意力、自注意力、位置编码)及实例与测试
  • 京东商品详情接口终极突破:从多接口联动解析到数据全息重构
  • 实用指南:On-Page SEO完全指南:从关键词策略到内容优化
  • 2025年品质卓越的羊毛地毯品牌综合推荐与选购指南