Skip to content

Commit

Permalink
1100
Browse files Browse the repository at this point in the history
  • Loading branch information
lzl124631x committed Feb 16, 2022
1 parent 6ed71dc commit 3943e32
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
1095 | Find in Mountain Array | Hard | [Solution](leetcode/1095.%20Find%20in%20Mountain%20Array)
1096 | Brace Expansion II | Hard | [Solution](leetcode/1096.%20Brace%20Expansion%20II)
1099 | Two Sum Less Than K | Easy | [Solution](leetcode/1099.%20Two%20Sum%20Less%20Than%20K)
1100 | Find K-Length Substrings With No Repeated Characters | Medium | [Solution](leetcode/1100.%20Find%20K-Length%20Substrings%20With%20No%20Repeated%20Characters)
1101 | The Earliest Moment When Everyone Become Friends | Medium | [Solution](leetcode/1101.%20The%20Earliest%20Moment%20When%20Everyone%20Become%20Friends)
1102 | Path With Maximum Minimum Value | Medium | [Solution](leetcode/1102.%20Path%20With%20Maximum%20Minimum%20Value)
1103 | Distribute Candies to People | Easy | [Solution](leetcode/1103.%20Distribute%20Candies%20to%20People)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# [1100. Find K-Length Substrings With No Repeated Characters (Medium)](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)

<p>Given a string <code>s</code> and an integer <code>k</code>, return <em>the number of substrings in </em><code>s</code><em> of length </em><code>k</code><em> with no repeated characters</em>.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre><strong>Input:</strong> s = "havefunonleetcode", k = 5
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are 6 substrings they are: 'havef','avefu','vefun','efuno','etcod','tcode'.
</pre>

<p><strong>Example 2:</strong></p>

<pre><strong>Input:</strong> s = "home", k = 5
<strong>Output:</strong> 0
<strong>Explanation:</strong> Notice k can be larger than the length of s. In this case, it is not possible to find any substring.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>


**Companies**:
[Amazon](https://leetcode.com/company/amazon)

**Related Topics**:
[Hash Table](https://leetcode.com/tag/hash-table/), [String](https://leetcode.com/tag/string/), [Sliding Window](https://leetcode.com/tag/sliding-window/)

## Solution 1. Fixed-length Sliding Window

```cpp
// OJ: https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
int numKLenSubstrNoRepeats(string s, int k) {
int N = s.size(), ans = 0, cnt[26] = {}, unique = 0;
for (int i = 0; i < N; ++i) {
if (++cnt[s[i] - 'a'] == 1) ++unique;
if (i - k >= 0 && --cnt[s[i - k] - 'a'] == 0) --unique;
ans += unique == k;
}
return ans;
}
};
```

0 comments on commit 3943e32

Please sign in to comment.