• 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 仅包含小写字母

    一开始我仍采用之前哈希使用的累加和的办法,但是出现了一些问题:

    会有不同的字符串加起来和相同。

    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* findAnagrams(char * s, char * p, int* returnSize){
    5. int len=strlen(p);
    6. int lens=strlen(s);
    7. int tar=0;
    8. *returnSize=0;
    9. int* result=(int*)malloc(lens*sizeof(int));
    10. for(int i=0;i
    11. tar+=(int)p[i];
    12. }
    13. for(int j=0;j<=lens-len;j++){
    14. int tars=0;
    15. for(int i=0;i
    16. tars+=(int)s[j+i];
    17. }
    18. if(tars==tar)
    19. {
    20. result[*returnSize]=j;
    21. (*returnSize)++;
    22. }
    23. }
    24. return result;
    25. }

    改进:

    引入一个单词表来记录:

    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* findAnagrams(char * s, char * p, int* returnSize){
    5. int len = strlen(p);
    6. int lens = strlen(s);
    7. *returnSize = 0;
    8. int* result = (int*)malloc(lens * sizeof(int));
    9. int tar = 0;
    10. int p_count[256] = {0};
    11. int s_count[256] = {0};
    12. for(int i = 0; i < len; i++){
    13. p_count[(int)p[i]]++;
    14. tar += (int)p[i];
    15. }
    16. for(int j = 0; j <= lens - len; j++){
    17. int tars = 0;
    18. memset(s_count, 0, sizeof(s_count));
    19. for(int i = 0; i < len; i++){
    20. s_count[(int)s[j + i]]++;
    21. tars += (int)s[j + i];
    22. }
    23. if(tars == tar && memcmp(p_count, s_count, sizeof(p_count)) == 0) {
    24. result[*returnSize] = j;
    25. (*returnSize)++;
    26. }
    27. }
    28. // Reallocate memory to fit the actual size of the result array
    29. result = (int*)realloc(result, (*returnSize) * sizeof(int));
    30. return result;
    31. }

    使用滑动窗口和计数排序

    写的时候一直报错,后来发现,居然有lens

    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* findAnagrams(char * s, char * p, int* returnSize){
    5. int len = strlen(p);
    6. int lens = strlen(s);
    7. *returnSize = 0;
    8. int* result = (int*)malloc(lens * sizeof(int));
    9. if (len > lens) {
    10. return result;
    11. }
    12. // Initialize character count arrays
    13. int p_count[26] = {0};
    14. int s_count[26] = {0};
    15. for(int i = 0; i < len; i++){
    16. p_count[p[i] - 'a']++;
    17. s_count[s[i] - 'a']++;
    18. }
    19. for(int j = 0; j <= lens - len; j++){
    20. if(memcmp(p_count, s_count, sizeof(p_count)) == 0) {
    21. result[*returnSize] = j;
    22. (*returnSize)++;
    23. }
    24. // Slide the window
    25. if(j + len < lens){
    26. s_count[s[j] - 'a']--;
    27. s_count[s[j + len] - 'a']++;
    28. }
    29. }
    30. // Reallocate memory to fit the actual size of the result array
    31. result = (int*)realloc(result, (*returnSize) * sizeof(int));
    32. return result;
    33. }

  • 相关阅读:
    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