给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。
异位词 指由相同字母重排列形成的字符串(包括相同的字符串)。
示例 1:
输入: s = "cbaebabacd", p = "abc" 输出: [0,6] 解释: 起始索引等于 0 的子串是 "cba", 它是 "abc" 的异位词。 起始索引等于 6 的子串是 "bac", 它是 "abc" 的异位词。
示例 2:
输入: s = "abab", p = "ab" 输出: [0,1,2] 解释: 起始索引等于 0 的子串是 "ab", 它是 "ab" 的异位词。 起始索引等于 1 的子串是 "ba", 它是 "ab" 的异位词。 起始索引等于 2 的子串是 "ab", 它是 "ab" 的异位词。
提示:
1 <= s.length, p.length <= 3 * 104s 和 p 仅包含小写字母一开始我仍采用之前哈希使用的累加和的办法,但是出现了一些问题:
会有不同的字符串加起来和相同。
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int* findAnagrams(char * s, char * p, int* returnSize){
- int len=strlen(p);
- int lens=strlen(s);
- int tar=0;
- *returnSize=0;
- int* result=(int*)malloc(lens*sizeof(int));
- for(int i=0;i
- tar+=(int)p[i];
- }
- for(int j=0;j<=lens-len;j++){
- int tars=0;
- for(int i=0;i
- tars+=(int)s[j+i];
- }
- if(tars==tar)
- {
- result[*returnSize]=j;
- (*returnSize)++;
- }
- }
- return result;
- }

改进:
引入一个单词表来记录:
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int* findAnagrams(char * s, char * p, int* returnSize){
- int len = strlen(p);
- int lens = strlen(s);
- *returnSize = 0;
- int* result = (int*)malloc(lens * sizeof(int));
-
- int tar = 0;
- int p_count[256] = {0};
- int s_count[256] = {0};
-
- for(int i = 0; i < len; i++){
- p_count[(int)p[i]]++;
- tar += (int)p[i];
- }
-
- for(int j = 0; j <= lens - len; j++){
- int tars = 0;
- memset(s_count, 0, sizeof(s_count));
-
- for(int i = 0; i < len; i++){
- s_count[(int)s[j + i]]++;
- tars += (int)s[j + i];
- }
-
- if(tars == tar && memcmp(p_count, s_count, sizeof(p_count)) == 0) {
- result[*returnSize] = j;
- (*returnSize)++;
- }
- }
-
- // Reallocate memory to fit the actual size of the result array
- result = (int*)realloc(result, (*returnSize) * sizeof(int));
-
- return result;
- }
使用滑动窗口和计数排序
写的时候一直报错,后来发现,居然有lens
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int* findAnagrams(char * s, char * p, int* returnSize){
- int len = strlen(p);
- int lens = strlen(s);
- *returnSize = 0;
- int* result = (int*)malloc(lens * sizeof(int));
-
- if (len > lens) {
- return result;
- }
-
- // Initialize character count arrays
- int p_count[26] = {0};
- int s_count[26] = {0};
-
- for(int i = 0; i < len; i++){
- p_count[p[i] - 'a']++;
- s_count[s[i] - 'a']++;
- }
-
- for(int j = 0; j <= lens - len; j++){
- if(memcmp(p_count, s_count, sizeof(p_count)) == 0) {
- result[*returnSize] = j;
- (*returnSize)++;
- }
-
- // Slide the window
- if(j + len < lens){
- s_count[s[j] - 'a']--;
- s_count[s[j + len] - 'a']++;
- }
- }
-
- // Reallocate memory to fit the actual size of the result array
- result = (int*)realloc(result, (*returnSize) * sizeof(int));
-
- return result;
- }

-
相关阅读:
ES6模块化练习import,export
分汤000
前端系列——HTML
前端反卷计划-组件库-01-环境搭建
李宏毅老师《机器学习》课程笔记-4.1 Self-attention
Java创建对象的最佳方式:单例模式(Singleton)
vue3 iconify 图标几种使用 并加载本地 svg 图标
使用Python的turtle模块绘制美丽的樱花树
Linux quotaoff命令教程:如何关闭文件系统的配额(附实例详解和注意事项)
Redis常见面试题
-
原文地址:https://blog.csdn.net/m0_61636632/article/details/138013759