【力扣 - 找到字符串中所有字母异位词】

news2024/7/4 2:42:56

题目描述

给定两个字符串 sp,找到 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 * 10^4
sp 仅包含小写字母

题解 - 滑动窗口

思路

根据题目要求,我们需要在字符串 s 寻找字符串 p 的异位词。因为字符串 p 的异位词的长度一定与字符串 p 的长度相同,所以我们可以在字符串 s 中构造一个长度为与字符串 p 的长度相同的滑动窗口,并在滑动中维护窗口中每种字母的数量;当窗口中每种字母的数量与字符串 p 中每种字母的数量相同时,则说明当前窗口为字符串 p 的异位词。

算法

在算法的实现中,我们可以使用数组来存储字符串 p 和滑动窗口中每种字母的数量。

细节

当字符串 s 的长度小于字符串 p 的长度时,字符串 s 中一定不存在字符串 p 的异位词。但是因为字符串 s 中无法构造长度与字符串 p 的长度相同的窗口,所以这种情况需要单独处理。
在这里插入图片描述

代码

/**
 * Function to check if a string is a match with a given character array
 * @param s: input string
 * @param len: length of the input string
 * @param vat: character array to match against
 * @return true if the string is a match, false otherwise
 */
bool stringIsMatch(char *s, int len, char *vat) {
    int i;
    char vatBak[26] = {0}; // Array to store character counts of the input string
    int temp;
    
    // Count the occurrences of each character in the input string
    for (i = 0; i < len; i++) {
    // For each character in the string  p , 
    // the ASCII value of the character is subtracted from the ASCII value of the character 'a'. 
    // This calculation results in a value between 0 and 25, 
    // which is used as an index to access the corresponding element in the vat array. 
        temp = s[i] - 'a';
        // After calculating the index  temp , 
        // the code increments the value stored at index  temp  in the  vat  array. 
        // This effectively counts the occurrences of each character in the string  p  and stores the counts in the  vat  array. 
        vatBak[temp]++;
    }
    
    // Compare the character counts with the given character array
    for (i = 0; i < 26; i++) {
        if (vat[i] != vatBak[i]) {
            return false; // If counts don't match, return false
        }
    }
    
    return true; // If all counts match, return true
}

/**
 * Function to find all anagrams of a given string in another string
 * @param s: input string
 * @param p: string to find anagrams of
 * @param returnSize: pointer to store the size of the result array
 * @return an array of indices where anagrams are found
 */
int* findAnagrams(char *s, char *p, int *returnSize) {
    char vat[26] = {0}; // Array to store character counts of the anagram string
    int i;
    int temp = 0;
    *returnSize = 0;
    int *returnNums = (int *)malloc(sizeof(int) * strlen(s)); // Allocate memory for result array
    
    // If the input string is shorter than the anagram string, return empty result
    if (strlen(s) < strlen(p)) {
        return returnNums;
    }
    
    // Count the occurrences of each character in the anagram string
    for (i = 0; i < strlen(p); i++) {
        temp = p[i] - 'a';
        vat[temp]++;
    }
    
    // Iterate through the input string to find anagrams
    for (i = 0; i <= (strlen(s) - strlen(p)); i++) {
        // Check if the substring starting at index i is an anagram
        // In the code snippet where  `s + i`  is used instead of  `s[i]` , 
        // the expression  `s + i`  is a pointer arithmetic operation that calculates the memory address of the  `i` -th element after the memory address of the base pointer  `s` . 
        // This is because in C, when you add an integer  `i`  to a pointer  `s` , 
        // the result is a pointer that points to the memory location  `i`  elements away from the original memory location pointed to by  `s` .
        // In this specific context,  `s + i`  is used to create a pointer 
        // to a substring of the input string  `s`  starting from index  `i` . 
        // This pointer is then passed to the  `stringIsMatch`  function 
        // to check if this substring is an anagram of the target string  `p` . 
        // By using pointer arithmetic, the code efficiently works with substrings of the input string 
        // without needing to create a separate substring array, 
        // thereby optimizing memory usage and performance.
        if (stringIsMatch(s + i, strlen(p), vat)) {
            returnNums[*returnSize] = i; // Store the index of the anagram
            *returnSize = *returnSize + 1; // Increment the size of the result array
        }
    }
    
    return returnNums; // Return the array of indices where anagrams are found
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1511016.html

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

相关文章

能查看二十四节气并提醒的软件是什么 二十四节气提醒软件

阳光斜照&#xff0c;我站在窗前&#xff0c;感受着四季的变迁。每个季节都有它独特的韵味&#xff0c;而二十四节气&#xff0c;便是这四季变换的细腻注脚。它们不仅是大自然的节奏&#xff0c;更与农事、生活紧密相连&#xff0c;承载着古人的智慧和对自然的敬畏。 小时候&a…

linux 新增定时任务

1、创建定时任务 crontab -e 2、加入定时任务规则 0 2 * * * /usr1/local/mysql-backup/backup.sh 说明&#xff1a;backup.sh是sh脚本 3、重启定时任务 service crond restart 扩展 1、查看定时任务列表 crontab -l 2、需要修改定时任务 crontab -e

第十八届全国大学生智能汽车竞赛——摄像头算法(附带个人经验)

文章目录 前言一、摄像头图像处理1、摄像头图像采集2、图像二值化与大津算法 二、左右边界&#xff0c;中线扫描 前言 参加了第十六&#xff0c;十七和第十八届全国大学生智能车竞赛&#xff0c;对摄像头的学习有部分心得&#xff0c;分享给大家&#xff0c;三届车赛&#xff…

explain关键字的用法(mysql高级部分)

文章目录 简介explain关键字分析 简介 explain主要是用来分析sql语句的&#xff0c;当你的系统中出现慢查询SQL后&#xff0c;你可以使用explain关键字对该语句进行分析。通过使用explain&#xff0c;我们可以得到以下结果 表的读取顺序 哪些索引可能使用 哪些索引被实际使用…

有c语言基础,如何快速学会C++核心知识?

有c语言基础&#xff0c;如何快速学会C核心知识&#xff1f; 在开始前我分享下我的经历&#xff0c;我刚入行时遇到一个好公司和师父&#xff0c;给了我机会&#xff0c;一年时间从3k薪资涨到18k的&#xff0c; 我师父给了一些 电气工程师学习方法和资料&#xff0c;让我不断提…

9:00面试,9:06就出来了,问的实在是太变态了

我从一家小公司转投到另一家公司&#xff0c;期待着新的工作环境和机会。然而&#xff0c;新公司的加班文化让我有些始料未及。虽然薪资相对较高&#xff0c;但长时间的工作和缺乏休息使我身心俱疲。 就在我逐渐适应这种高强度的工作节奏时&#xff0c;公司突然宣布了一则令人…

力扣● 1143.最长公共子序列 ● 1035.不相交的线 ● 53. 最大子序和 动态规划

● 1143.最长公共子序列 1.dp数组含义。 dp[i][j]&#xff1a;数组1[0,i-1]范围的子数组和数组2[0,j-1]的子数组的公共子序列最长长度。注意这里不需要一定以A[i-1]/B[j-1]结尾&#xff0c;原因在下面有说明。 动态规划求子序列的问题&#xff0c;一般都是dp的下标相对于数组…

系统运维网络知识汇总

一、系统运维中网络方面的规划与思考 系统运维建立在网络的基础之上&#xff0c;如果没有一个相对合理的网络架构&#xff0c;恐怕系统运维做起来也不是那么的顺手。一个公司基本上都会把网络和服务器独立开来&#xff0c;划分不同的区域摆放设备&#xff0c;很多时候都是物理…

YOLOv8_seg-Openvino和ONNXRuntime推理【CPU】

纯检测系列&#xff1a; YOLOv5-Openvino和ONNXRuntime推理【CPU】 YOLOv6-Openvino和ONNXRuntime推理【CPU】 YOLOv8-Openvino和ONNXRuntime推理【CPU】 YOLOv7-Openvino和ONNXRuntime推理【CPU】 YOLOv9-Openvino和ONNXRuntime推理【CPU】 跟踪系列&#xff1a; YOLOv5/6/7-O…

力扣L5----- 58. 最后一个单词的长度(2024年3月11日)

1.题目 2.知识点 注1&#xff1a; lastIndexOf()它用于查找指定字符或子字符串在当前字符串中最后一次出现的位置。它的作用是从字符串的末尾向前搜索指定字符或子字符串&#xff0c;并返回其最后一次出现的位置的索引。 &#xff08;1&#xff09;例如&#xff0c;在字符串 …

银河麒麟V10SP3操作系统-网络时间配置

1、动态网络配置 打开终端&#xff0c;以网口 eth0 为例&#xff1a; nmcli conn add connection.id eth0-dhcp type ether ifname eth0 ipv4.method auto其中“eth0-dhcp”为连接的名字&#xff0c;可以根据自己的需要命名方便记忆和操作 的名字&#xff1b;“ifname eth0”…

鞋服品牌如何计算门店盈亏平衡?

在鞋服品牌的运营中&#xff0c;门店盈亏平衡是衡量门店经营效果的重要指标。盈亏平衡点意味着门店在达到这一销售水平时&#xff0c;既能够覆盖所有固定和变动成本&#xff0c;又能实现零利润或零亏损。计算门店盈亏平衡有助于品牌更好地理解门店的经营状况&#xff0c;制定合…

Springboot进行web开发

创建springboot工程&#xff0c;基于2022版idea pom.xml文件中的插件爆红&#xff1a; 解决方法&#xff1a;给插件加<version>版本号</version> 版本号和<parent></parent>中的版本号一样。 另外有人说重启也可以解决爆红&#xff0c;可以试一下&a…

SpringBoot(容器功能)

文章目录 1.Configuration 添加/注入bean1.注入bean1.编写一个JavaBean&#xff0c;Monster.java2.创建一个config文件夹&#xff08;名字任意&#xff09;&#xff0c;用于存放配置Bean的类&#xff08;相当于配置文件&#xff09;3.BeanConfig.java4.测试使用 MainApp.java2.…

高中信息技术教资学习

一、几种排序方法的基本思想 1、直接插入排序&#xff08;假设按照从小到大进行排序&#xff09; 默认第一个元素是有序的&#xff0c;从有序的元素末尾开始&#xff0c;与要插入的元素进行比较&#xff0c;如果要插入的元素比有序的末尾元素小的话&#xff0c;就将有序末尾元…

适合一个人开的实体店:创业新选择与经营秘籍大公开

大家好&#xff0c;我是一名开鲜奶吧5年的实体店创业者&#xff0c;在行业里摸爬滚打多年&#xff0c;积累了丰富的经验。今天&#xff0c;我想和大家分享一些关于适合一个人开的实体店的创业新选择和经营秘籍。 首先&#xff0c;我们来聊一聊适合一个人开的实体店有哪些。这类…

多线程案例及常用模式

一.单例模式——经典的设计模式 什么是单例模式&#xff1a;就是规定一个类只能创建一个对象&#xff0c;也就是保证某个类在程序中只存在唯一一个实例&#xff0c;而不会创建出多个实例 根据对象创建的时机不同&#xff0c;可以分为饿汉模式和懒汉模式 1.饿汉模式 在类加载…

基与HTML5的塔防游戏设计与实现

目 录 摘 要 I Abstract II 引 言 1 1 项目背景与相关技术 3 1.1 背景与发展简介 3 1.2 HTML5技术及其优势 4 1.3 JavaScript开发的优势与劣势 4 1.4 CSS样式表在开发中的用处 5 1.5 本章小结 6 2 系统分析 7 2.1 需求分析 7 2.2 问题分析 7 2.3 流程设计 7 2.3 功能分析 8 2.…

【Git】Github 上commit后,绿格子contribution却不显示?不知道怎么弥补?解决方法在这里

github 上commit后&#xff0c;绿格子&#xff08;contribution&#xff09;却不显示 问题描述 今天一直在github上面commit代码&#xff0c;但是github中并没有显示自己的contribution&#xff08;没有绿色的格子&#xff09;&#xff0c;全是空白&#xff0c;网上一查是因为…

点一下即可任意调整静态图片:这个开源AI图片项目你需要了解一下

项目简介 合成满足用户需求的视觉内容通常需要对生成对象的姿势、形状、表情和布局进行灵活而精确的控制。现有的方法通过手动注释的训练数据或先前的3D模型来获得生成对抗网络&#xff08;GAN&#xff09;的可控性&#xff0c;这通常缺乏灵活性、精确性和通用性。在这项工作中…