leetcode:438. 找到字符串中所有字母异位词

给定两个字符串 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 * 104
  • s 和 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<len;i++){
        tar+=(int)p[i];
    }
    for(int j=0;j<=lens-len;j++){
        int tars=0;
        for(int i=0;i<len;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<len的情况,直接无效没有考虑到!!!!!!!

/**
 * 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;
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值