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

news/2025/11/16 17:13:31/文章来源:https://www.cnblogs.com/yangykaifa/p/19228518

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.mzph.cn/news/967205.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

11 月 11 日

今日进度 (1)Commit 记录 •陈鉴祥: 优化 Redis 配置,完成 mcp-svc 分布式锁基础逻辑 •何绍斌: 解决 order-svc N+1 问题,编写 pay-svc 支付单逻辑 •张廷智: 添加账单页加载动画,完成报修页表单组件 •郑权:…

2025年国内烘干技术厂家排行榜:十大优质供应商深度评测

摘要 随着烘干技术行业的快速发展,2025年国内烘干设备市场呈现出智能化、节能化、安全化的明显趋势。本文基于市场调研和用户反馈,对市面上主流烘干技术厂家进行综合排名,为有采购需求的用户提供参考。文章包含详细…

2025年烘干技术源头厂家推荐排行榜前十名

摘要 烘干技术行业在2025年持续快速发展,尤其在节能环保和智能化方面取得显著进展。本文基于市场调研和用户反馈,整理了市面上烘干技术源头厂家的排行榜,旨在为采购决策提供参考。榜单综合考量了厂家实力、技术创新…

Docmost部署与应用实践

Docmost部署与应用实践Docmost 简介 Docmost是一款开源的协作维基和文档管理软件,它旨在为团队提供一个集中化、高效且易于使用的平台来创建、共享以及管理信息。作为 Confluence 和 Notion 的开源替代品,Docmost …

[论文笔记] Lifting On-Demand Analysis to Higher-Order Languages

Introduction 很多静态分析工具在进行按需静态分析之前都假设存在一个调用图。但是这种假设不够好,对于 JavaScript 这种具有多种动态特性的语言,调用图分析和数据流分析之间的相互依赖关系要更强。如果忽略这种相互…

2025年烘干机厂家排行榜前十强推荐:行业精选与选择指南

摘要 随着农业机械和食品加工行业的快速发展,烘干机设备在2025年迎来技术创新高峰,注重安全、节能和智能化。本文基于行业数据和用户反馈,整理出2025年烘干机厂家排行榜前十强,为采购决策提供参考。榜单结合推荐指…

Java 可变参数机制

基本语法和使用 1. 基本语法public class VarargsExample {// 可变参数声明:类型... 参数名public static int sum(int... numbers) {int total = 0;for (int num : numbers) {total += num;}return total;}// 可变参…

11 月 3 日

项目核心信息链接信息类型 链接地址 备注说明 团队博客(阶段计划) https://www.cnblogs.com/zq-zgcai/p/19191312 含 Alpha 阶段目标、分工及风险管控 Github 代码仓库 https://github.com/hotfixMyLife/House-Renta…

002 vue3-admin项目的目录及文件说明之src目录及其子目录、子文件

整体目录结构src/ ├── api/ # API 接口相关 ├── assets/ # 静态资源 ├── components/ # 通用组件 ├── composables/ # 组合式函数 ├── constants/ …

全国性搬家公司推荐榜:从运费优势、专业度、靠谱口碑、费用便宜划算等综合实力排名

在搬家需求日益多元化的当下,选择一家综合实力过硬的搬家公司成为不少消费者的难题。本推荐榜聚焦运费优势、专业服务、省心口碑、费用性价比四大核心维度,对全国性搬家公司平台进行综合评估。最终精选出6家表现突出…

C# 高级类型 Dictionary(学习笔记4)

key-value方式,key不可以重复,value可以。namespace WindowsFormsApp1 {public partial class Form1 : Form{public Form1(){InitializeComponent();}private static Dictionary<int, string> dictionary = ne…

Java 垃圾收集机制

垃圾收集的基本概念 垃圾收集(Garbage Collection, GC) 是 JVM 自动管理内存的机制,负责回收不再使用的对象所占用的内存。 关键概念垃圾对象:不再被任何引用指向的对象GC Roots:始终可达的对象,作为引用链的起点…

Metasploit Framework 6.4.99 (macOS, Linux, Windows) - 开源渗透测试框架

Metasploit Framework 6.4.99 (macOS, Linux, Windows) - 开源渗透测试框架Metasploit Framework 6.4.99 (macOS, Linux, Windows) - 开源渗透测试框架 Rapid7 Penetration testing, updated November 15, 2025 请访问…

小程序获取OCR识别结果,示例代码

//获取OCR识别结果,示例代码--startwx.chooseMedia({count: 1,mediaType: [image],sourceType: [album, camera],maxDuration: 30,camera: back,success: (res) => {if(res.errMsg=="chooseMedia:ok"){l…

20232405 2024-2025-1 《网络与系统攻防技术》实验五实验报告

20232405 2024-2025-1 《网络与系统攻防技术》实验五实验报告1. 实验内容(1)对任意一个DNS域名进行查询并获取一些信息(2)查询任意社交媒体中好友的ip,并获取其所在地理位置(3)使用nmap对靶机环境进行扫描并获取…

Invicti v25.11 发布,新增功能简介

Invicti v25.11 发布,新增功能简介Invicti v25.11 发布,新增功能简介 Invicti v25.11.0 for Windows - Web 应用程序安全测试 Invicti (formerly Netsparker) | Web Application and API Security for Enterprise 请…

Acunetix v25.11 发布,新增功能简介

Acunetix v25.11 发布,新增功能简介Acunetix v25.11 发布,新增功能简介 Acunetix v25.11.0 (Linux, Windows) - Web 应用程序安全测试 Acunetix | Web Application Security Scanner 请访问原文链接:https://sysin.…

【运维自动化-标准运维】变量的高级用法

在全局变量使用篇里了解到了各类变量的基本用法,实际在很多场景下,需要对变量进行处理,这就是标准运维里变量的高级用法。只要处理变量符合python语法,在一行代码语句的长度内可以执行的代码,系统均是可以进行渲染…

MySQL数据过滤与计算字段实战技术指南

MySQL数据过滤与计算字段实战技术指南一、数据过滤进阶:多条件组合与高效筛选 在MySQL数据检索中,精准过滤数据是提升查询效率与结果有效性的核心环节。通过组合WHERE子句及专用操作符,可实现复杂业务场景下的数据筛…

2025-11-14 PQ v.Next日志记录

2025-11-14 PQ v.Next日志记录 在李剑老师的帮助下,PQ v1.1.9版本目前已上线,因此需要重新更新一下用户使用手册。 https://z.gitee.cn/zgca/projects/777586/repos/zgca/aipq/sources今日进度(4*4):基于最新版本…