From 2d505ccf1394c033e8441649aa573cabeb9d69d5 Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Mon, 6 Feb 2023 14:52:24 +0400 Subject: [PATCH] add leetcode Permutation in String (#1207) * add leetcode Permutation in String * updating DIRECTORY.md * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update leetcode/src/567.c Co-authored-by: Stepfen Shawn * Update 567.c fix review notes * Update 567.c remove redundant line --------- Co-authored-by: github-actions[bot] Co-authored-by: Stepfen Shawn --- leetcode/DIRECTORY.md | 1 + leetcode/src/567.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 leetcode/src/567.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 8e0d621b..a66c0a82 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -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 | diff --git a/leetcode/src/567.c b/leetcode/src/567.c new file mode 100644 index 00000000..268579a0 --- /dev/null +++ b/leetcode/src/567.c @@ -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; +}