Skip to content

Commit ddf6c8e

Browse files
committed
apr-19
1 parent dfdf8d0 commit ddf6c8e

14 files changed

+867
-89
lines changed

README.md

Lines changed: 20 additions & 11 deletions
Large diffs are not rendered by default.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Tag: Hash Table, String, Sliding Window
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a string text. You can swap two of the characters in the text.
8+
// Return the length of the longest substring with repeated characters.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: text = "ababa"
13+
// Output: 3
14+
// Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.
15+
//
16+
// Example 2:
17+
//
18+
// Input: text = "aaabaaa"
19+
// Output: 6
20+
// Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.
21+
//
22+
// Example 3:
23+
//
24+
// Input: text = "aaaaa"
25+
// Output: 5
26+
// Explanation: No need to swap, longest repeated character substring is "aaaaa" with length is 5.
27+
//
28+
//  
29+
// Constraints:
30+
//
31+
// 1 <= text.length <= 2 * 104
32+
// text consist of lowercase English characters only.
33+
//
34+
//
35+
36+
class Solution {
37+
public:
38+
int maxRepOpt1(string text) {
39+
unordered_map<char, int> counter;
40+
for (auto &ch: text) {
41+
counter[ch] += 1;
42+
}
43+
int res = 0;
44+
for (auto &[k, c]: counter) {
45+
int tmp = min(c, count(text, k));
46+
res = max(res, tmp);
47+
}
48+
return res;
49+
}
50+
51+
int count(string &text, char target) {
52+
int n = text.size();
53+
int i = 0;
54+
int res = 0;
55+
int count = 0;
56+
for (int j = 0; j < n; j++) {
57+
count += text[j] != target;
58+
while (count > 1) {
59+
count -= text[i] != target;
60+
i += 1;
61+
}
62+
res = max(res, j - i + 1);
63+
}
64+
return res;
65+
}
66+
};
67+
68+
class Solution {
69+
public:
70+
int maxRepOpt1(string text) {
71+
int n = text.size();
72+
vector<pair<int, int>> group;
73+
vector<int> counter(26, 0);
74+
int count = 1;
75+
for (int i = 0; i < n; i++) {
76+
counter[text[i] - 'a'] += 1;
77+
if (i == n - 1 || text[i] != text[i + 1]) {
78+
group.emplace_back(text[i] - 'a', count);
79+
count = 1;
80+
} else {
81+
count += 1;
82+
}
83+
}
84+
85+
int res = 0;
86+
for (auto &[x, count]: group) {
87+
res = max(res, min(count + 1, counter[x]));
88+
}
89+
90+
for (int i = 1; i < group.size() - 1; i++) {
91+
if (group[i - 1].first == group[i + 1].first && group[i].second == 1) {
92+
res = max(res, min(group[i - 1].second + group[i + 1].second + 1, counter[group[i - 1].first]));
93+
}
94+
}
95+
return res;
96+
}
97+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Tag: Hash Table, String, Sliding Window
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a string text. You can swap two of the characters in the text.
8+
# Return the length of the longest substring with repeated characters.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: text = "ababa"
13+
# Output: 3
14+
# Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa" with length 3.
15+
#
16+
# Example 2:
17+
#
18+
# Input: text = "aaabaaa"
19+
# Output: 6
20+
# Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa" with length 6.
21+
#
22+
# Example 3:
23+
#
24+
# Input: text = "aaaaa"
25+
# Output: 5
26+
# Explanation: No need to swap, longest repeated character substring is "aaaaa" with length is 5.
27+
#
28+
#  
29+
# Constraints:
30+
#
31+
# 1 <= text.length <= 2 * 104
32+
# text consist of lowercase English characters only.
33+
#
34+
#
35+
36+
from collections import Counter
37+
class Solution:
38+
def maxRepOpt1(self, text: str) -> int:
39+
res = 0
40+
counter = Counter(text)
41+
for k in counter:
42+
count = min(counter[k], self.count(text, k))
43+
res = max(res, count)
44+
return res
45+
46+
def count(self, text: str, target: str) -> int:
47+
n = len(text)
48+
res = 0
49+
i = 0
50+
count = 0
51+
for j in range(n):
52+
count += text[j] != target
53+
while count > 1:
54+
count -= text[i] != target
55+
i += 1
56+
57+
res = max(res, j - i + 1)
58+
return res
59+
60+
from collections import defaultdict
61+
class Solution:
62+
def maxRepOpt1(self, text: str) -> int:
63+
n = len(text)
64+
group = []
65+
counter = defaultdict(int)
66+
count = 1
67+
for i in range(n):
68+
counter[text[i]] += 1
69+
if i == n - 1 or text[i] != text[i + 1]:
70+
group.append([text[i], count])
71+
count = 1
72+
else:
73+
count += 1
74+
75+
res = 0
76+
for x, count in group:
77+
res = max(res, min(count + 1, counter[x]))
78+
79+
for i in range(1, len(group) - 1):
80+
if group[i - 1][0] == group[i + 1][0] and group[i][1] == 1:
81+
res = max(res, min(group[i - 1][1] + group[i + 1][1] + 1, counter[group[i - 1][0]]))
82+
83+
return res
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Tag: Array
2+
// Time: O(N^2)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/OiRTe8BDOR0
7+
8+
// Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: nums = [3,1,2,2,2,1,3], k = 2
13+
// Output: 4
14+
// Explanation:
15+
// There are 4 pairs that meet all the requirements:
16+
// - nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
17+
// - nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
18+
// - nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
19+
// - nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
20+
//
21+
// Example 2:
22+
//
23+
// Input: nums = [1,2,3,4], k = 1
24+
// Output: 0
25+
// Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= nums.length <= 100
31+
// 1 <= nums[i], k <= 100
32+
//
33+
//
34+
35+
class Solution {
36+
public:
37+
int countPairs(vector<int>& nums, int k) {
38+
int n = nums.size();
39+
int res = 0;
40+
for (int i = 0; i < n; i++) {
41+
for (int j = i + 1; j < n; j++) {
42+
if (nums[i] == nums[j] && i * j % k == 0) {
43+
res += 1;
44+
}
45+
}
46+
}
47+
48+
return res;
49+
}
50+
};
51+
52+
class Solution {
53+
public:
54+
int countPairs(vector<int>& nums, int k) {
55+
int n = nums.size();
56+
int res = 0;
57+
unordered_map<int, vector<int>> m;
58+
for (int i = 0; i < n; ++i)
59+
m[nums[i]].push_back(i);
60+
61+
for (auto &[x, ids] : m) {
62+
unordered_map<int, int> gcds;
63+
for (auto i : ids) {
64+
auto gcd_i = gcd(i, k);
65+
for (auto &[gcd_j, cnt] : gcds)
66+
res += gcd_i * gcd_j % k ? 0 : cnt;
67+
gcds[gcd_i] += 1;
68+
}
69+
}
70+
return res;
71+
}
72+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Tag: Array
2+
# Time: O(N^2)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/OiRTe8BDOR0
7+
8+
# Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: nums = [3,1,2,2,2,1,3], k = 2
13+
# Output: 4
14+
# Explanation:
15+
# There are 4 pairs that meet all the requirements:
16+
# - nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
17+
# - nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
18+
# - nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
19+
# - nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
20+
#
21+
# Example 2:
22+
#
23+
# Input: nums = [1,2,3,4], k = 1
24+
# Output: 0
25+
# Explanation: Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# 1 <= nums.length <= 100
31+
# 1 <= nums[i], k <= 100
32+
#
33+
#
34+
35+
class Solution:
36+
def countPairs(self, nums: List[int], k: int) -> int:
37+
n = len(nums)
38+
res = 0
39+
for i in range(n):
40+
for j in range(i + 1, n):
41+
if nums[i] == nums[j] and i * j % k == 0:
42+
res += 1
43+
44+
return res
45+
46+
from collections import defaultdict
47+
import math
48+
class Solution:
49+
def countPairs(self, nums: List[int], k: int) -> int:
50+
n = len(nums)
51+
indexes = defaultdict(list)
52+
for i in range(n):
53+
indexes[nums[i]].append(i)
54+
55+
res = 0
56+
for x, ids in indexes.items():
57+
gcds = defaultdict(int)
58+
for i in ids:
59+
gcd_i = math.gcd(i, k)
60+
for gcd_j, count in gcds.items():
61+
res += count if gcd_i * gcd_j % k == 0 else 0
62+
gcds[gcd_i] += 1
63+
return res

leetcode/2537.count-the-number-of-good-subarrays.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Space: O(N)
44
// Ref: -
55
// Note: -
6+
// Video: https://youtu.be/WUVFxJR_EB8
67

78
// Given an integer array nums and an integer k, return the number of good subarrays of nums.
89
// A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].

leetcode/2537.count-the-number-of-good-subarrays.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Space: O(N)
44
# Ref: -
55
# Note: -
6+
# Video: https://youtu.be/WUVFxJR_EB8
67

78
# Given an integer array nums and an integer k, return the number of good subarrays of nums.
89
# A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].

0 commit comments

Comments
 (0)