438 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".

解析:给定两个字符串,s为主串,p为子串,求出s中包含p的同字母异次序的所有起始位置。其中题目的要求是:在s中子串的字母必须和p的字母种类和数量保持一致,无顺序要求。因此我们可以统计p中字符的出现次数,然后在s中从头开始遍历,每次在s中找p字符串长度个字符,然后验证字符个数是否相等。

具体过程:定义一个长度为256的hash数组(ascii码范围),数组的索引表示字符的ascii值,记录p中字符出现的次数。然后定义两个变量left和right表示滑动窗口的左右边界,定义变量count表示字符串p中需要匹配的字符个数(即p的长度)。然后开始遍历s,在窗口右移过程中,如果right位置的字符已经在哈希表中了,说明该字符在p中有出现,则count减1,然后哈希表中该字符个数自减1,右边界right加1;如果此时count等于0,说明p中的字符在s的子串中都匹配上了,那么将此时左边界left加入结果res中;如果此时right和left的差为p的长度,说明此时应该去掉最左边的一个字符,我们看如果该字符在哈希表中的个数大于等于0,说明该字符是p中的字符。因为上面我们有让每个字符减1,如果不是p中的字符,那么在哈希表中个数应该为0,自减1后就为-1,所以这样就知道该字符是否属于p,如果我们去掉了属于p的一个字符,count自增1。

public class FindAllAnagramsinaString438 {
	public static void main(String[] args) {
		String s = "cbaebabacd";
		String p = "abc";
		System.out.println(findAnagrams(s, p));
	}
	public static List<Integer> findAnagrams(String s, String p) {
    	ArrayList<Integer> res = new ArrayList<Integer>();
    	// 定义hash表
    	int[] hash = new int[256];
    	for(int i=0;i<p.length();i++){
    		hash[p.charAt(i)]++;
    	}
    	int left=0;
    	int right=0;
    	int count=p.length();
    	while(right<s.length()){
    		// 窗口右移,相应的hash值减小,如果这个位置的Hash值是正的,
    		// 表示p字符串也包含这个, 所以count-- 
    		if(hash[s.charAt(right++)]-->0) count--;
    		// count为0表示s的子串和p对应的hash值完全一致
    		if(count==0) res.add(left);
    		// 如果当窗口大小一定的时候即窗口大小和需要比较的字符串大小一致的时候
    		// 将窗口左边的指针向右边移动,移动的同时左边的字符计数因为在第一个if的地方hash值减小过
    		// 所以需要执行对应恢复操作,即:hash值增加,count值增加
    		if(right-left==p.length()&&hash[s.charAt(left++)]++>=0) count++;
    	}
        return res;
    }
}




MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集和测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集和测试集(默认7:3)。 数据归一化:使用mapminmax将输入和输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
全排列是指将一个字符串中的字符进行重新排列,使得所有可能的排列都被考虑到。对于给定的字符串s,可以使用递归的方法来实现全排列。首先,选择一个字符作为排列的第一个字符,然后对剩余的字符进行全排列。这个过程可以通过不断交换字符的位置来实现。具体的代码实现可以参考以下示例代码: ```java public class StringPermutation { public void permutation(String s) { char\[\] array = s.toCharArray(); permutationHelper(array, 0, array.length - 1); } private void permutationHelper(char\[\] array, int start, int end) { if (start >= end) { System.out.println(new String(array)); } else { for (int i = start; i <= end; i++) { swap(array, i, start); permutationHelper(array, start + 1, end); swap(array, i, start); } } } private void swap(char\[\] array, int i, int j) { char temp = array\[i\]; array\[i\] = array\[j\]; array\[j\] = temp; } public static void main(String\[\] args) { StringPermutation permutation = new StringPermutation(); String s = "abc"; permutation.permutation(s); } } ``` 这段代码会输出字符串"abc"的所有全排列,即"abc"、"acb"、"bac"、"bca"、"cab"、"cba"。\[2\] 需要注意的是,全排列的时间复杂度为O(n!),其中n为字符串的长度。因此,对于较长的字符串,全排列可能会非常耗时。如果只需要判断一个字符串是否是另一个字符串的排列,可以使用类似于438. Find All Anagrams in a String的方法,只需要比较两个字符串中长度相同的子字符串所包含的字符是否一样即可。\[1\] #### 引用[.reference_title] - *1* [[LeetCode] 567. Permutation in String 字符串中的全排列](https://blog.youkuaiyun.com/weixin_30802171/article/details/97515238)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [字符串全排列问题](https://blog.youkuaiyun.com/weixin_41876155/article/details/81869743)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值