当前位置: 首页 > 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.zskr.cn/news/51339.html

相关文章:

  • 2025年国内烘干技术厂家排行榜:十大优质供应商深度评测
  • 2025年烘干技术源头厂家推荐排行榜前十名
  • Docmost部署与应用实践
  • 11 月 3 日
  • 002 vue3-admin项目的目录及文件说明之src目录及其子目录、子文件
  • Java 垃圾收集机制
  • 20232405 2024-2025-1 《网络与系统攻防技术》实验五实验报告
  • 【运维自动化-标准运维】变量的高级用法
  • 详细介绍:K8s 安全机制全解析
  • 详细介绍:MySQL索引指南
  • Java 设计模式—— 责任链模式:从原理到 SpringBoot 最优搭建
  • 京东商品详情接口终极突破:从多接口联动解析到数据全息重构
  • 2025年品质卓越的羊毛地毯品牌综合推荐与选购指南
  • 20232415 2025-2026-1 《网络与系统攻防技术》 实验五实验报告
  • CSP2025反思——于诗涵
  • 接雨水算法全解析:从错误到3种最优解法(含扩展与思路Trigger)
  • C#性能优化基础:高CPU使用率(trace)
  • 详细介绍:Linux Bash(一)
  • pytest测试range内置函数
  • WPS---功能设置
  • [Debug记录] 分布式实验-FTP编程
  • 2025年国内旧房翻新公司综合实力排行榜TOP10推荐
  • Linux服务器编程实践60-双向管道:socketpair函数的完成与应用场景
  • 循环数组下一个更大元素:从错误到精通(含2种解法+同类型扩展)
  • 实验四运行结果
  • 随机化数论算法总结
  • 20232422 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • 完整教程:【数据迁移】HBase Bulkload批量加载原理
  • 【AI智能体开发】什么是LLM?如何在本地搭建属于自己的Ai智能体? - 详解
  • DL 1 深度学习简介 张量tensor操作