[LeetCode]Find All Anagrams in a String

[LeetCode]Find All Anagrams in a String

大家好,又见面了,我是全栈君。

Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.

Example 1:

Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:

Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

1.解题思路

anagrams,就是只顺序不同但个数相同的字符串,那我们就可以利用hashtable的思想来比较每个字符串中字符出现的个数是否相等。
对于两个字符串我们分别准备数组(大小为256)来存储每个字符出现的次数:
1) 对于p,我们遍历,并在hp中记录字符出现的次数;
2) 之后遍历s,先把当前字符的个数+1,但是需要考虑当前index是否已经超过了p的长度,如果超过,则表示前面的字符已经不予考虑,所以要将index-plen的字符的个数-1;最后判断两个数组是否相等,如果相等,返回index-plen+1,即为开始的下标。

2.代码

public class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> res=new ArrayList<Integer>();
        if(s.length()==0||s==null||p.length()==0||p==null) return res;
        int[] hs=new int[256];
        int[] hp=new int[256];
        int plen=p.length();
        for(int i=0;i<plen;i++){
            hp[p.charAt(i)]++;
        }
        for(int j=0;j<s.length();j++){
            hs[s.charAt(j)]++;
            if(j>=plen){
                hs[s.charAt(j-plen)]--;
            }
            if(Arrays.equals(hs,hp))
                res.add(j-plen+1);
        }
        return res;
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108772.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android开发:仿美团下拉列表菜单,帮助类,复用简单

    Android开发:仿美团下拉列表菜单,帮助类,复用简单

    2022年2月7日
    116
  • oracle笛卡尔积「建议收藏」

    oracle笛卡尔积「建议收藏」笛卡尔积定义:多张表相乘,简单的说就是两个集合相乘的积。(列数相加,行数相乘)先建了两张表,课程表:信息表:1.内连接innerjoinon 内连接:笛卡尔积中,只显示有关联的数据,无关联不显示。select*fromlefttable左表,righttable右表where左表.主键=右表.外键;select*fromlefttable左表innerjoin右表…

    2022年7月11日
    42
  • Mysql存储过程基本语法

    Mysql存储过程基本语法Mysql存储过程基本语法转载自这篇文章delimiter//一般情况下MYSQL以;结尾表示确认输入并执行语句,但在存储过程中;不是表示结束,因此可以用该命令将;号改为//表示确认输入并执行。一.创建存储过程1.基本语法:createproceduresp_name()begin………end2.参数传递二.调用存储过程1.基本语法:ca…

    2022年7月17日
    15
  • oracle dmp导入导出_oracle导出数据

    oracle dmp导入导出_oracle导出数据Oracle数据导入导出imp/exp就相当于oracle数据还原与备份。exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中。利用这个功能可以构建两个相同的数据库,一个用来测试,一个用来正式使用……Oracle数据导入导出imp/exp就相当于oracle数据还原与备份。exp命令可以把数据从远程数据库服务器导出到本地的dmp…

    2025年6月28日
    2
  • Python学习【第五篇】循环语句「建议收藏」

    Python学习【第五篇】循环语句「建议收藏」Python循环语句接下来将介绍Python的循环语句,程序在一般情况下是按顺序执行的。编程语言提供了各种控制结构,允许更复杂的执行路径。循环语句允许我们执行一个语句或语句组多次。Python

    2022年7月5日
    16
  • TensorFlow绘制loss/accuracy曲线[通俗易懂]

    TensorFlow绘制loss/accuracy曲线[通俗易懂]1.多曲线1.1使用pyplot方式importnumpyasnpimportmatplotlib.pyplotaspltx=np.arange(1,11,1)plt.plot(x,x*2,label=”First”)plt.plot(x,x*3,label=”Second”)plt.plot(x,x*4,label=”Thi…

    2025年7月26日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号