mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
add leetcode Permutation in String (#1207)
* add leetcode Permutation in String * updating DIRECTORY.md * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn <m18824909883@163.com> * Update 567.c fix review notes * Update 567.c remove redundant line --------- Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com> Co-authored-by: Stepfen Shawn <m18824909883@163.com>
This commit is contained in:
parent
4830210f69
commit
2d505ccf13
@ -91,6 +91,7 @@
|
||||
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number) | [C](./src/509.c) | Easy |
|
||||
| 520 | [Detect Capital](https://leetcode.com/problems/detect-capital) | [C](./src/520.c) | Easy |
|
||||
| 561 | [Array Partition](https://leetcode.com/problems/array-partition) | [C](./src/561.c) | Easy |
|
||||
| 567 | [Permutation in String](https://leetcode.com/problems/permutation-in-string) | [C](./src/567.c) | Medium |
|
||||
| 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees) | [C](./src/617.c) | Easy |
|
||||
| 647 | [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings) | [C](./src/647.c) | Medium |
|
||||
| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree) | [C](./src/669.c) | Medium |
|
||||
|
67
leetcode/src/567.c
Normal file
67
leetcode/src/567.c
Normal file
@ -0,0 +1,67 @@
|
||||
const int EnglishLettersNumber = 26;
|
||||
|
||||
void countCharsForStringSlice(int* charsCounter, char* s, int length, int sign) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
|
||||
charsCounter[s[i] - 'a'] += sign;
|
||||
}
|
||||
}
|
||||
|
||||
// Sliding window
|
||||
// Calculate number of chars in the current slide.
|
||||
// Runtime: O(n)
|
||||
// Space: O(1) - only number of english lowercase letters.
|
||||
bool checkInclusion(char* s1, char* s2) {
|
||||
int lengthS1 = strlen(s1);
|
||||
int lengthS2 = strlen(s2);
|
||||
|
||||
if (lengthS1 > lengthS2) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int* charsCounter = calloc(EnglishLettersNumber, sizeof(int));
|
||||
|
||||
// We keep counters of s1 with '-' sign. It has to be offset by s2 chars
|
||||
countCharsForStringSlice(charsCounter, s1, lengthS1, -1);
|
||||
countCharsForStringSlice(charsCounter, s2, lengthS1, 1);
|
||||
|
||||
int diffChars = 0;
|
||||
for (int i = 0; i < EnglishLettersNumber; i++) {
|
||||
if (charsCounter[i] != 0) {
|
||||
diffChars++;
|
||||
}
|
||||
}
|
||||
|
||||
if (diffChars == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < lengthS2 - lengthS1; i++) {
|
||||
int charNumberLeft = s2[i] - 'a';
|
||||
int charNumberRight = s2[i + lengthS1] - 'a';
|
||||
|
||||
charsCounter[charNumberLeft] -= 1;
|
||||
if (charsCounter[charNumberLeft] == 0) {
|
||||
diffChars -= 1;
|
||||
}
|
||||
else if (charsCounter[charNumberLeft] == -1) {
|
||||
diffChars += 1;
|
||||
}
|
||||
|
||||
charsCounter[charNumberRight] += 1;
|
||||
if (charsCounter[charNumberRight] == 0) {
|
||||
diffChars -= 1;
|
||||
}
|
||||
else if (charsCounter[charNumberRight] == 1) {
|
||||
diffChars += 1;
|
||||
}
|
||||
|
||||
if (diffChars == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
free(charsCounter);
|
||||
return false;
|
||||
}
|
Loading…
Reference in New Issue
Block a user