← Home

567. Permutation in String

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
sliding windowC++Markdown
567

The trick here is to name the state correctly, then let the implementation follow. For 567. Permutation in String, the solution in this repository is mainly a sliding window solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: sliding window.

The notes already sitting in the source point us in the right direction:

  • hashmap + sliding window

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are checkInclusion, s1Count.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//hashmap + sliding window
02//Runtime: 24 ms, faster than 42.09% of C++ online submissions for Permutation in String.
03//Memory Usage: 7.2 MB, less than 100.00% of C++ online submissions for Permutation in String.
04class Solution {
05public:
06    bool checkInclusion(string s1, string s2) {
07        vector<int> s1Count(26, 0), windowCount(26, 0);
08        
09        for(char c : s1){
10            s1Count[c-'a'] += 1;
11        }
12        
13        for(int i = 0; i < s2.size(); i++){
14            if(i < s1.size()){
15                windowCount[s2[i]-'a'] += 1;
16            }else{
17                windowCount[s2[i]-'a'] += 1;
18                windowCount[s2[i-s1.size()]-'a'] -= 1;
19            }
20            
21            if(s1Count == windowCount){
22                return true;
23            }
24        }
25        
26        return false;
27    }
28};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.