From 1d99852943142cfd67474f7c630ebb4cf9935bb3 Mon Sep 17 00:00:00 2001 From: pranay999000 Date: Sat, 2 Apr 2022 17:04:40 +0530 Subject: [PATCH 1/4] feat: add solution to lc problem: No. 0395 --- .../README.md | 87 +++++++++++++++++++ .../README_EN.md | 87 +++++++++++++++++++ .../Solution.java | 48 ++++++++++ .../Solution.py | 39 +++++++++ 4 files changed, 261 insertions(+) create mode 100644 solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java create mode 100644 solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.py diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md index 24254b61783b2..95ece7da8953b 100644 --- a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md @@ -46,6 +46,45 @@ ```python +class Solution: + def longestSubstring(self, s: str, k: int) -> int: + n = len(s) + maxLength = 0 + + for i in range(1, 27): + counter = dict() + left = 0 + right = 0 + unique = 0 + kCount = 0 + + while right < n: + if unique <= i: + r = s[right] + counter[r] = counter.get(r, 0) + 1 + + if counter[r] == 1: + unique += 1 + if counter[r] == k: + kCount += 1 + + right += 1 + + else: + l = s[left] + counter[l] = counter.get(l, 0) - 1 + + if counter[l] == 0: + unique -= 1 + if counter[l] == k-1: + kCount -= 1 + + left += 1 + + if unique == i and kCount == i: + maxLength = max(maxLength, right-left) + + return maxLength ``` @@ -54,6 +93,54 @@ ```java +public class Solution { + public static int longestSubstring(String s, int k) { + int maxLength = 0; + int n = s.length(); + + for (int i=1; i<27; i++) { + HashMap counter = new HashMap<>(); + int left = 0; + int right = 0; + int unique = 0; + int kCount = 0; + + while (right < n) { + if (unique <= i) { + char r = s.charAt(right); + if (counter.containsKey(r)) + counter.put(r, counter.get(r) + 1); + else + counter.put(r, 1); + + if (counter.get(r) == 1) + unique += 1; + if (counter.get(r) == k) + kCount += 1; + + right += 1; + } else { + char l = s.charAt(left); + if (counter.containsKey(l)) + counter.put(l, counter.get(l) - 1); + else + counter.put(l, 1); + + if (counter.get(l) == 0) + unique -= 1; + if (counter.get(l) == k-1) + kCount -= 1; + + left += 1; + } + + if (unique == i && kCount == i) + maxLength = Math.max(maxLength, right - left); + } + } + return maxLength; + } +} ``` diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md index 1cf00910ba486..55a1f805f9abc 100644 --- a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md @@ -39,12 +39,99 @@ ### **Python3** ```python +class Solution: + def longestSubstring(self, s: str, k: int) -> int: + n = len(s) + maxLength = 0 + + for i in range(1, 27): + counter = dict() + left = 0 + right = 0 + unique = 0 + kCount = 0 + + while right < n: + if unique <= i: + r = s[right] + counter[r] = counter.get(r, 0) + 1 + + if counter[r] == 1: + unique += 1 + if counter[r] == k: + kCount += 1 + + right += 1 + + else: + l = s[left] + counter[l] = counter.get(l, 0) - 1 + + if counter[l] == 0: + unique -= 1 + if counter[l] == k-1: + kCount -= 1 + + left += 1 + + if unique == i and kCount == i: + maxLength = max(maxLength, right-left) + + return maxLength ``` ### **Java** ```java +public class Solution { + public static int longestSubstring(String s, int k) { + int maxLength = 0; + int n = s.length(); + + for (int i=1; i<27; i++) { + HashMap counter = new HashMap<>(); + int left = 0; + int right = 0; + int unique = 0; + int kCount = 0; + + while (right < n) { + if (unique <= i) { + char r = s.charAt(right); + if (counter.containsKey(r)) + counter.put(r, counter.get(r) + 1); + else + counter.put(r, 1); + + if (counter.get(r) == 1) + unique += 1; + if (counter.get(r) == k) + kCount += 1; + + right += 1; + } else { + char l = s.charAt(left); + if (counter.containsKey(l)) + counter.put(l, counter.get(l) - 1); + else + counter.put(l, 1); + + if (counter.get(l) == 0) + unique -= 1; + if (counter.get(l) == k-1) + kCount -= 1; + + left += 1; + } + + if (unique == i && kCount == i) + maxLength = Math.max(maxLength, right - left); + } + } + return maxLength; + } +} ``` diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java new file mode 100644 index 0000000000000..9d115120fbdbd --- /dev/null +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java @@ -0,0 +1,48 @@ +public class Solution { + public static int longestSubstring(String s, int k) { + int maxLength = 0; + int n = s.length(); + + for (int i=1; i<27; i++) { + HashMap counter = new HashMap<>(); + int left = 0; + int right = 0; + int unique = 0; + int kCount = 0; + + while (right < n) { + if (unique <= i) { + char r = s.charAt(right); + if (counter.containsKey(r)) + counter.put(r, counter.get(r) + 1); + else + counter.put(r, 1); + + if (counter.get(r) == 1) + unique += 1; + if (counter.get(r) == k) + kCount += 1; + + right += 1; + } else { + char l = s.charAt(left); + if (counter.containsKey(l)) + counter.put(l, counter.get(l) - 1); + else + counter.put(l, 1); + + if (counter.get(l) == 0) + unique -= 1; + if (counter.get(l) == k-1) + kCount -= 1; + + left += 1; + } + + if (unique == i && kCount == i) + maxLength = Math.max(maxLength, right - left); + } + } + return maxLength; + } +} diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.py b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.py new file mode 100644 index 0000000000000..05c4fcfeaa372 --- /dev/null +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.py @@ -0,0 +1,39 @@ +class Solution: + def longestSubstring(self, s: str, k: int) -> int: + n = len(s) + maxLength = 0 + + for i in range(1, 27): + counter = dict() + left = 0 + right = 0 + unique = 0 + kCount = 0 + + while right < n: + if unique <= i: + r = s[right] + counter[r] = counter.get(r, 0) + 1 + + if counter[r] == 1: + unique += 1 + if counter[r] == k: + kCount += 1 + + right += 1 + + else: + l = s[left] + counter[l] = counter.get(l, 0) - 1 + + if counter[l] == 0: + unique -= 1 + if counter[l] == k-1: + kCount -= 1 + + left += 1 + + if unique == i and kCount == i: + maxLength = max(maxLength, right-left) + + return maxLength From 20f084dcfd246fb5c3a766ee547a70f980cccd4f Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sun, 3 Apr 2022 09:50:02 +0800 Subject: [PATCH 2/4] Update README.md --- .../README.md | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md index 95ece7da8953b..3bb43488a2fc6 100644 --- a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README.md @@ -85,7 +85,6 @@ class Solution: maxLength = max(maxLength, right-left) return maxLength - ``` ### **Java** @@ -93,13 +92,13 @@ class Solution: ```java -public class Solution { - public static int longestSubstring(String s, int k) { +class Solution { + public int longestSubstring(String s, int k) { int maxLength = 0; int n = s.length(); - for (int i=1; i<27; i++) { - HashMap counter = new HashMap<>(); + for (int i = 1; i < 27; i++) { + Map counter = new HashMap<>(); int left = 0; int right = 0; int unique = 0; @@ -108,40 +107,33 @@ public class Solution { while (right < n) { if (unique <= i) { char r = s.charAt(right); - if (counter.containsKey(r)) - counter.put(r, counter.get(r) + 1); - else - counter.put(r, 1); - - if (counter.get(r) == 1) + counter.put(r, counter.getOrDefault(r, 0) + 1); + if (counter.get(r) == 1) { unique += 1; - if (counter.get(r) == k) + } + if (counter.get(r) == k) { kCount += 1; - + } right += 1; } else { char l = s.charAt(left); - if (counter.containsKey(l)) - counter.put(l, counter.get(l) - 1); - else - counter.put(l, 1); - - if (counter.get(l) == 0) + counter.put(l, counter.getOrDefault(l, 2) - 1); + if (counter.get(l) == 0) { unique -= 1; - if (counter.get(l) == k-1) + } + if (counter.get(l) == k - 1) { kCount -= 1; - + } left += 1; } - - if (unique == i && kCount == i) + if (unique == i && kCount == i) { maxLength = Math.max(maxLength, right - left); + } } } return maxLength; } } - ``` ### **...** From e41069d219efaf5fb7ac560f099109836177a471 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sun, 3 Apr 2022 09:50:18 +0800 Subject: [PATCH 3/4] Update Solution.java --- .../Solution.java | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java index 9d115120fbdbd..b3b42004f388e 100644 --- a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/Solution.java @@ -1,10 +1,10 @@ -public class Solution { - public static int longestSubstring(String s, int k) { +class Solution { + public int longestSubstring(String s, int k) { int maxLength = 0; int n = s.length(); - for (int i=1; i<27; i++) { - HashMap counter = new HashMap<>(); + for (int i = 1; i < 27; i++) { + Map counter = new HashMap<>(); int left = 0; int right = 0; int unique = 0; @@ -13,34 +13,28 @@ public static int longestSubstring(String s, int k) { while (right < n) { if (unique <= i) { char r = s.charAt(right); - if (counter.containsKey(r)) - counter.put(r, counter.get(r) + 1); - else - counter.put(r, 1); - - if (counter.get(r) == 1) + counter.put(r, counter.getOrDefault(r, 0) + 1); + if (counter.get(r) == 1) { unique += 1; - if (counter.get(r) == k) + } + if (counter.get(r) == k) { kCount += 1; - + } right += 1; } else { char l = s.charAt(left); - if (counter.containsKey(l)) - counter.put(l, counter.get(l) - 1); - else - counter.put(l, 1); - - if (counter.get(l) == 0) + counter.put(l, counter.getOrDefault(l, 2) - 1); + if (counter.get(l) == 0) { unique -= 1; - if (counter.get(l) == k-1) + } + if (counter.get(l) == k - 1) { kCount -= 1; - + } left += 1; } - - if (unique == i && kCount == i) + if (unique == i && kCount == i) { maxLength = Math.max(maxLength, right - left); + } } } return maxLength; From 03f2136aa2a731761878607f7792dca5b4ccd0a5 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Sun, 3 Apr 2022 09:51:25 +0800 Subject: [PATCH 4/4] Update README_EN.md --- .../README_EN.md | 40 ++++++++----------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md index 55a1f805f9abc..488276fbce894 100644 --- a/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md +++ b/solution/0300-0399/0395.Longest Substring with At Least K Repeating Characters/README_EN.md @@ -78,19 +78,18 @@ class Solution: maxLength = max(maxLength, right-left) return maxLength - ``` ### **Java** ```java -public class Solution { - public static int longestSubstring(String s, int k) { +class Solution { + public int longestSubstring(String s, int k) { int maxLength = 0; int n = s.length(); - for (int i=1; i<27; i++) { - HashMap counter = new HashMap<>(); + for (int i = 1; i < 27; i++) { + Map counter = new HashMap<>(); int left = 0; int right = 0; int unique = 0; @@ -99,40 +98,33 @@ public class Solution { while (right < n) { if (unique <= i) { char r = s.charAt(right); - if (counter.containsKey(r)) - counter.put(r, counter.get(r) + 1); - else - counter.put(r, 1); - - if (counter.get(r) == 1) + counter.put(r, counter.getOrDefault(r, 0) + 1); + if (counter.get(r) == 1) { unique += 1; - if (counter.get(r) == k) + } + if (counter.get(r) == k) { kCount += 1; - + } right += 1; } else { char l = s.charAt(left); - if (counter.containsKey(l)) - counter.put(l, counter.get(l) - 1); - else - counter.put(l, 1); - - if (counter.get(l) == 0) + counter.put(l, counter.getOrDefault(l, 2) - 1); + if (counter.get(l) == 0) { unique -= 1; - if (counter.get(l) == k-1) + } + if (counter.get(l) == k - 1) { kCount -= 1; - + } left += 1; } - - if (unique == i && kCount == i) + if (unique == i && kCount == i) { maxLength = Math.max(maxLength, right - left); + } } } return maxLength; } } - ``` ### **...**