当前位置: 首页 > news >正文

leedcode:找到字符串中所有字母异位词

问题:给定两个字符串 s 和 p,找到 s 中所有 p 的 异位词 的子串,返回这些子串的起始索引。不考虑答案输出的顺序。

package com.text;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;import com.alibaba.fastjson.JSONObject;public class findAnagrams {public static void main(String[] args) {findAnagrams("cbaebabacd","abc");}public static List<Integer> findAnagrams(String s, String p) {List<Integer> list=new ArrayList<Integer>();int slen=s.length();int plen=p.length();//p的长度大于s就返回if(plen>slen){return list;}//将p的字符存储到map中,key是字符,value是出现的次数Map<Character,Integer> pMap=new HashMap<Character,Integer>();for(int i=0;i<p.length();i++){Integer pNum=pMap.getOrDefault(p.charAt(i), 0);pMap.put(p.charAt(i),pNum+1);}//截取s串的起始坐标int endIndex=0;//存储截取的S串中字符出现的次数Map<Character,Integer> sMap=new HashMap<Character,Integer>();while(endIndex+plen<=slen){String substr= s.substring(endIndex,endIndex+plen);//下标等于0的时候需要全量存储if(endIndex==0){for(int i=0;i<substr.length();i++){Integer sNum=sMap.getOrDefault(substr.charAt(i), 0);sMap.put(substr.charAt(i),sNum+1);}}else{//不等于0的时候相当于下标右移//前一个元素出现次数需要减1,如果次数等于0需要移除,否则影响相等的校验//最后一个需要加1char removeKey = s.charAt(endIndex-1);Integer sNum=sMap.getOrDefault(removeKey, 0);if(sNum-1==0) {sMap.remove(removeKey);}else {sMap.put(removeKey,sNum-1);}char addKey = substr.charAt(plen-1);Integer sNum1=sMap.getOrDefault(addKey, 0);sMap.put(addKey,sNum1+1);}//判断是否相等两个map//会进行key和value的全变比较if(sMap.equals(pMap)) {list.add(endIndex);}endIndex++;}System.out.println(JSONObject.toJSONString(list));return list;}}


文章转载自:
http://antemeridian.hyyxsc.cn
http://bin.hyyxsc.cn
http://antileukemia.hyyxsc.cn
http://biddy.hyyxsc.cn
http://acidifier.hyyxsc.cn
http://biodynamic.hyyxsc.cn
http://bughunter.hyyxsc.cn
http://astrophysics.hyyxsc.cn
http://amphitryon.hyyxsc.cn
http://ananas.hyyxsc.cn
http://anarthria.hyyxsc.cn
http://barometer.hyyxsc.cn
http://avigator.hyyxsc.cn
http://bouquetin.hyyxsc.cn
http://bymotive.hyyxsc.cn
http://berliner.hyyxsc.cn
http://biquinary.hyyxsc.cn
http://auscultation.hyyxsc.cn
http://calcic.hyyxsc.cn
http://brunhild.hyyxsc.cn
http://bellhanger.hyyxsc.cn
http://cancroid.hyyxsc.cn
http://boresome.hyyxsc.cn
http://antiestablishment.hyyxsc.cn
http://atmologist.hyyxsc.cn
http://absquatulater.hyyxsc.cn
http://cem.hyyxsc.cn
http://adscript.hyyxsc.cn
http://ablator.hyyxsc.cn
http://bushwhacking.hyyxsc.cn
http://www.dtcms.com/a/263363.html

相关文章:

  • 04密码加密
  • 中钧科技参加中亚数字经济对话会,引领新疆企业数字化新征程!
  • 【Teensy】在ArduinoIDE中配置Teensy4.1
  • LoRA 实战指南:NLP 与 CV 场景的高效微调方法全解析
  • 非常详细版: dd.device.geolocation 钉钉微应用获取定位,移动端 PC端都操作,Vue实现钉钉微应用获取精准定位并渲染在地图组件上
  • 强化学习概述及学习流程
  • 视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
  • spring-ai 工作流
  • LG 将正式终止手机相关服务,彻底告别手机市场
  • 机器人、灵巧手动捕方案 | 突破底层适配,动捕数据直通仿真平台
  • 【科研绘图系列】R语言绘制世界地图分布(world map)
  • 【数据挖掘】数据挖掘综合案例—银行精准营销
  • 衡石科技chatbot分析手册--钉钉数据问答机器人配置
  • 纯前端本地文件管理器(VSCode风格)(浏览器对本地文件增删改查)
  • 阿里云计算巢私有化MCP市场:企业级AI工具的安全部署新选择
  • hono+postgresql+CURD
  • 华为云Flexus+DeepSeek征文 | 从零开始搭建Dify-LLM应用开发平台:华为云全流程单机部署实战教程
  • FPGA实现CameraLink视频解码转SDI输出,基于LVDS+GTX架构,提供2套工程源码和技术支持
  • 如何让Excel自动帮我们算加减乘除?
  • Python 数据分析与机器学习入门 (一):环境搭建与核心库概览
  • DAY 43 预训练模型
  • RWKV-8 系列之 DeepEmbedAttention:精简 KV 缓存,尤其适合混合模型(RWKV-7s)
  • 创客匠人方法论:从 0 到 1 打造创始人 IP 的实战框架
  • pytorch底层原理学习--PyTorch 架构梳理
  • 3.前端和后端参数不一致,后端接不到数据的解决方案
  • 16014.rtsp推流服务器
  • 详解Kafka如何保证消息可靠性
  • 大语言模型(LLM)笔记
  • YOLOv12_ultralytics-8.3.145_2025_5_27部分代码阅读笔记-instance.py
  • [15-2] 读写内部FLASH读取芯片ID 江协科技学习笔记(20个知识点)