leetcode 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:

  1. The input strings only contain lower case letters.
  2. The length of both given strings is in range [1, 10,000].

维护一下一段区间的字符串就行,有O(26*N)的做法,我这里是用multiset做得,时间复杂度理论上  不必O(26*n)大啊但是跑的慢 可能是判断集合是否相等的时候比较费时间  有时间好好看看multiset源码

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        multiset<char>se1,se2;
        if (s2.size() < s1.size()) return false;
        for (int i = 0; i < s1.size(); ++i) se1.insert(s1[i]),se2.insert(s2[i]);
        if (se2 == se1) return true;

        for (int i = s1.size(); i < s2.size(); ++i) {
            char c = s2[i - s1.size()];
            auto x = se2.find(c);
            se2.erase(x);
            se2.insert(s2[i]);
            if (se1 == se2) return true;
        }
        return false;
    }
};
时间: 2024-10-11 17:44:11

leetcode 567. Permutation in String的相关文章

[滑动窗口/哈希] leetcode 567 Permutation in String

problem:https://leetcode.com/problems/permutation-in-string/ 这道题感觉几乎和Leetcode上另一题一模一样,昨天刚刷的:https://www.cnblogs.com/fish1996/p/11269526.html,就当签到题爽一爽了. class Solution { public: bool checkInclusion(string s1, string s2) { vector<int> target(26, 0); f

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

Leetcode:Next Permutation 下一个排列

Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending ord

leetcode第一刷_Scramble String

字符串的好题.题干解释的非常复杂,一下让人不知所措了. 这道题到底是什么意思呢?最终的结果是把一个字符串中字母的顺序打乱了,让你判断一个字符串能不能由另一个字符串打乱得到.那打乱这个过程是怎么做的呢,很简单,给你一个字符串,你必须先找一个点把它砍成两半,你可以通过交换这两半的顺序来打乱源字符串的顺序,也就是在两半中的字符与另一半中所有字符的相对顺序是统一的.对于每一半,都可以重复上面的过程. 那想一下,怎么知道打断的那个点在哪呢?穷举.怎么知道打断之后有没有做交换操作呢?两种情况递归,有一条走的

LeetCode OJ--Next Permutation *

求一个排列的下一个排列. 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1 #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: void nextPermutation(vector<int> &num) { if(num.size() == 0) return; c

LeetCode: Next Permutation [030]

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl

leetcode第一刷_Interleaving String

有关这种字符串的题真是层出不穷啊,而且他们都有这样一个特点,就是递归的思路如此简单,但一定超时! 这个时候,dp就朝我们缓缓走来.递归超,dp搞!这道题的状态转移方程还是比较好写的,用ispart[i][j]代表s1贡献i长,s2贡献j长时,能不能形成s3的前i+j个字符.更新可以按照行或者列开始,s3的前i+j个字符,可以是((i-1)+1)+j构成,也可以是i+((j-1)+1)构成,这取决于当前的这个字符s3[i+j-1]跟s1[i-1]和s2[j-1]是否相等,或的关系就可以解决问题.

Leetcode 344:Reverse String 反转字符串(python、java)

Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input string is given as an array of characters char[]. Do not allocate extra space for another array, you must do this by modifying the input array in-place wit

LeetCode | 1374. Generate a String With Characters That Have Odd Counts生成每种字符都是奇数个的字符串【Python】

LeetCode 1374. Generate a String With Characters That Have Odd Counts生成每种字符都是奇数个的字符串[Easy][Python][字符串] Problem LeetCode Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times. The