567. Permutation in String

2024-05-07 08:08
文章标签 string permutation 567

本文主要是介绍567. Permutation in String,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

567. Permutation in String

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string.

Example 1:
Input:s1 = “ab” s2 = “eidbaooo”
Output:True
Explanation: s2 contains one permutation of s1 (“ba”).
Example 2:
Input:s1= “ab” s2 = “eidboaoo”
Output: False
Note:
The input strings only contain lower case letters.
The length of both given strings is in range [1, 10,000].
先祭出来自己的低效率算法,low比算法,虽然过了,但是自己都很不满意。

bool checkInclusion1(string s1, string s2) {if (s1 == "" && s2 == "")return true;if (s1 == "" || s2 == "" || s1.size() > s2.size())return false;unordered_map<char, int> temp;for (auto x : s1)temp[x]++;for (int i = 0; i < s2.size(); i++){if (s1.find(s2[i]) != string::npos){unordered_map<char, int> tt = temp;if (s2.size() - i < s1.size())return false;string str = s2.substr(i, s1.size());for (int ii = 0; ii < s1.size(); ii++)  --tt[str[ii]];bool flag = false;for (auto x : tt){if (x.second < 0){flag = true;break;}}if (!flag)return true;}}return false;
}

算法时间复杂度为O(m*n),空间复杂度为O(2*m)

参考算法

bool checkInclusion(string s1, string s2) {if (s1.size() > s2.size())return false;int m = s1.size(), n = s2.size();vector<int> map1(26), map2(26);for (int i = 0; i < m; i++){map1[s1[i] - 'a']++;map2[s2[i] - 'a']++;}if (map1 == map2)return true;for (int i = 0; i + m < n; i++){map2[s2[i] - 'a']--;map2[s2[i + m] - 'a']++;if (map2 == map1)return true;}return false;
}

用了流动窗口的思想,虽然要求是全排列,但只要保证两个字串的每个元素的个数是相同就可以了。
时间复杂度为O(n),空间复杂度为O(n);

切记,一定要心情好的时候刷题,不然总是一头雾水,虽然做出来了,但是,效率低的自己都觉得不好意思。。。


这个题与上一个题目很类似

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”.

vector<int> findAnagrams(string s, string p) {vector<int> res;vector<int> map1(26), map2(26);int m = p.size(), n = s.size();for (int i = 0; i < m; i++){map1[p[i] - 'a']++;map2[s[i] - 'a']++;}int i = 0;for (; i + m < n; i++){if (map2 == map1)res.push_back(i);map2[s[i] - 'a']--;map2[s[i + m] - 'a']++; }//这里的 i != n 主要是针对 p 长度为1if (map2 == map1 && i != n)res.push_back(i);return res;
}

这篇关于567. Permutation in String的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966791

相关文章

string字符会调用new分配堆内存吗

gcc的string默认大小是32个字节,字符串小于等于15直接保存在栈上,超过之后才会使用new分配。

hdu2072(string的应用)

单词数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 25447    Accepted Submission(s): 5957 Problem Description lily的好朋友xiaoou333最近很空,他

【UVA】10739 - String to Palindrome(动态规划)

比较水的动态规划 dp[i][j] 将原串 i ~ j 之内的字符转化为回文字符所需要的最小操作次数 其中删除操作和添加操作本质上是一样的。 三个状态转移方程: dp[i][j] = min(dp[i][j] ,dp[i + 1][j]); dp[i][j] = min(dp[i][j] ,dp[i + 1][j - 1]); dp[i][j] = min(dp[i][j] ,dp[

理解String的compareTo()方法返回值

compareTo()的返回值是整型,它是先比较对应字符的大小(ASCII码顺序), 如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值。 如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符作比较, 以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度。 我们可以通过阅读源码加深对compareTo()的理解: comp

【JavaScript】基本数据类型与引用数据类型区别(及为什么String、Boolean、Number基本数据类型会有属性和方法?)

基本数据类型   JavaScript基本数据类型包括:undefined、null、number、boolean、string。基本数据类型是按值访问的,就是说我们可以操作保存在变量中的实际的值。 1)基本数据类型的值是不可变的 任何方法都无法改变一个基本类型的值,比如一个字符串: var name = "change";name.substr();//hangconsole.log

leetcode#541. Reverse String II

题目 Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of

Java中Map取值转String Null值处理

Map<String, Object> 直接取值转String String value = (String)map.get("key") 当map.get(“key”)为Null值时会报错。 使用String类的valueOf静态方法可以解决这个问题 String value = String.valueOf(map.get("key"))

Qt的QString和C++string之间的转换

QString qstr; string str; //将QString转化为C++的string str = qstr.toStdString(); //将C++的string转化为QString qstr = QString::fromStdString(str);

string类、string类的常用接口说明等的介绍

文章目录 前言一、 string类二、 string类的常用接口说明1. string类对象的常见构造2. string类对象的容量操作3. string类对象的访问及遍历操作4. string类对象的修改操作5. string类非成员函数 总结 前言 string类、string类的常用接口说明等的介绍 一、 string类 string是表示字符串的字符串类该类的