Skip to content

Commit 585eff8

Browse files
committed
Feb-17
1 parent fc8382f commit 585eff8

12 files changed

+558
-121
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Large diffs are not rendered by default.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Tag: Hash Table, String, Backtracking, Counting
2+
// Time: O(N!)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/Q2z9GbjkWmo
7+
8+
// You have n  tiles, where each tile has one letter tiles[i] printed on it.
9+
// Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: tiles = "AAB"
14+
// Output: 8
15+
// Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
16+
//
17+
// Example 2:
18+
//
19+
// Input: tiles = "AAABBC"
20+
// Output: 188
21+
//
22+
// Example 3:
23+
//
24+
// Input: tiles = "V"
25+
// Output: 1
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= tiles.length <= 7
31+
// tiles consists of uppercase English letters.
32+
//
33+
//
34+
35+
class Solution {
36+
public:
37+
int numTilePossibilities(string tiles) {
38+
int n = tiles.size();
39+
sort(tiles.begin(), tiles.end());
40+
vector<bool> used(n, false);
41+
vector<string> res;
42+
helper(tiles, used, res, "");
43+
44+
return res.size();
45+
}
46+
47+
void helper(string tiles, vector<bool> &used, vector<string> &res, string tmp) {
48+
for (int i = 0; i < tiles.size(); i++) {
49+
if (used[i]) {
50+
continue;
51+
}
52+
53+
if (i == 0 || tiles[i] != tiles[i - 1] || used[i - 1]) {
54+
used[i] = true;
55+
string sub = tmp + tiles[i];
56+
res.push_back(sub);
57+
helper(tiles, used, res, sub);
58+
used[i] = false;
59+
}
60+
}
61+
}
62+
};
63+
64+
class Solution {
65+
public:
66+
int res = 0;
67+
68+
int numTilePossibilities(string tiles) {
69+
unordered_map<char,int>map;
70+
for(char ch : tiles) {
71+
map[ch]++;
72+
}
73+
backtrack(map);
74+
return res;
75+
}
76+
77+
void backtrack(unordered_map<char, int>& map) {
78+
for(auto &[ch,count] : map) {
79+
if(count > 0) {
80+
res++;
81+
map[ch]--;
82+
backtrack(map);
83+
map[ch]++;
84+
}
85+
}
86+
}
87+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Tag: Hash Table, String, Backtracking, Counting
2+
# Time: O(N!)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/Q2z9GbjkWmo
7+
8+
# You have n  tiles, where each tile has one letter tiles[i] printed on it.
9+
# Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: tiles = "AAB"
14+
# Output: 8
15+
# Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".
16+
#
17+
# Example 2:
18+
#
19+
# Input: tiles = "AAABBC"
20+
# Output: 188
21+
#
22+
# Example 3:
23+
#
24+
# Input: tiles = "V"
25+
# Output: 1
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# 1 <= tiles.length <= 7
31+
# tiles consists of uppercase English letters.
32+
#
33+
#
34+
35+
class Solution:
36+
def numTilePossibilities(self, tiles: str) -> int:
37+
n = len(tiles)
38+
used = [False for i in range(n)]
39+
res = []
40+
41+
self.helper(sorted(tiles), used, res, "")
42+
return len(res)
43+
44+
def helper(self, tiles: list, used: list, res: list, tmp: str):
45+
for i in range(len(tiles)):
46+
if used[i]:
47+
continue
48+
49+
if i == 0 or tiles[i] != tiles[i - 1] or used[i - 1]:
50+
used[i] = True
51+
sub = tmp + tiles[i]
52+
res.append(sub)
53+
self.helper(tiles, used, res, sub)
54+
used[i] = False
55+
56+
from collections import Counter
57+
class Solution:
58+
def numTilePossibilities(self, tiles: str) -> int:
59+
self.res = 0
60+
freq = Counter(tiles)
61+
self.backtrack(freq)
62+
return self.res
63+
64+
def backtrack(self, freq):
65+
for ch in freq:
66+
if freq[ch] > 0:
67+
self.res += 1
68+
freq[ch] -= 1
69+
self.backtrack(freq)
70+
freq[ch] += 1
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Tag: String, Dynamic Programming, Greedy, Sliding Window
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a binary string s. You are allowed to perform two types of operations on the string in any sequence:
8+
//
9+
// Type-1: Remove the character at the start of the string s and append it to the end of the string.
10+
// Type-2: Pick any character in s and flip its value, i.e., if its value is '0' it becomes '1' and vice-versa.
11+
//
12+
// Return the minimum number of type-2 operations you need to perform such that s becomes alternating.
13+
// The string is called alternating if no two adjacent characters are equal.
14+
//
15+
// For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
16+
//
17+
//  
18+
// Example 1:
19+
//
20+
// Input: s = "111000"
21+
// Output: 2
22+
// Explanation: Use the first operation two times to make s = "100011".
23+
// Then, use the second operation on the third and sixth elements to make s = "101010".
24+
//
25+
// Example 2:
26+
//
27+
// Input: s = "010"
28+
// Output: 0
29+
// Explanation: The string is already alternating.
30+
//
31+
// Example 3:
32+
//
33+
// Input: s = "1110"
34+
// Output: 1
35+
// Explanation: Use the second operation on the second element to make s = "1010".
36+
//
37+
//  
38+
// Constraints:
39+
//
40+
// 1 <= s.length <= 105
41+
// s[i] is either '0' or '1'.
42+
//
43+
//
44+
45+
class Solution {
46+
public:
47+
int minFlips(string s) {
48+
int n = s.size();
49+
vector<int> counter = {0, 0};
50+
int outer = (n + 1) / 2;
51+
int inner = n - outer;
52+
int res = n;
53+
for (int i = 0; i < 2 * n; i++) {
54+
if (i >= n) {
55+
counter[(i - n) & 1] -= (s[i - n] - '0');
56+
}
57+
58+
counter[i & 1] += (s[i % n] - '0');
59+
if (i >= n - 1) {
60+
int k = (i - n + 1) % 2;
61+
int count = min(outer - counter[k] + counter[1 - k], counter[k] + inner - counter[1 - k]);
62+
res = min(res, count);
63+
}
64+
}
65+
66+
return res;
67+
}
68+
};
69+
70+
class Solution {
71+
public:
72+
int minFlips(string s) {
73+
int k = s.size();
74+
s += s;
75+
int n = s.size();
76+
string s1 = "", s2 = "";
77+
78+
for (int i = 0; i < n; ++i) {
79+
s1 += (i % 2 == 0) ? '1' : '0';
80+
s2 += (i % 2 == 1) ? '1' : '0';
81+
}
82+
83+
int res = k;
84+
int count1 = 0, count2 = 0;
85+
86+
87+
for (int i = 0; i < n; ++i) {
88+
if (i >= k) {
89+
if (s[i - k] != s1[i - k]) count1--;
90+
if (s[i - k] != s2[i - k]) count2--;
91+
}
92+
93+
if (s[i] != s1[i]) count1++;
94+
if (s[i] != s2[i]) count2++;
95+
96+
if (i >= k - 1) {
97+
res = min(res, min(count1, count2));
98+
}
99+
}
100+
101+
return res;
102+
}
103+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Tag: String, Dynamic Programming, Greedy, Sliding Window
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a binary string s. You are allowed to perform two types of operations on the string in any sequence:
8+
#
9+
# Type-1: Remove the character at the start of the string s and append it to the end of the string.
10+
# Type-2: Pick any character in s and flip its value, i.e., if its value is '0' it becomes '1' and vice-versa.
11+
#
12+
# Return the minimum number of type-2 operations you need to perform such that s becomes alternating.
13+
# The string is called alternating if no two adjacent characters are equal.
14+
#
15+
# For example, the strings "010" and "1010" are alternating, while the string "0100" is not.
16+
#
17+
#  
18+
# Example 1:
19+
#
20+
# Input: s = "111000"
21+
# Output: 2
22+
# Explanation: Use the first operation two times to make s = "100011".
23+
# Then, use the second operation on the third and sixth elements to make s = "101010".
24+
#
25+
# Example 2:
26+
#
27+
# Input: s = "010"
28+
# Output: 0
29+
# Explanation: The string is already alternating.
30+
#
31+
# Example 3:
32+
#
33+
# Input: s = "1110"
34+
# Output: 1
35+
# Explanation: Use the second operation on the second element to make s = "1010".
36+
#
37+
#  
38+
# Constraints:
39+
#
40+
# 1 <= s.length <= 105
41+
# s[i] is either '0' or '1'.
42+
#
43+
#
44+
45+
class Solution:
46+
def minFlips(self, s: str) -> int:
47+
k = len(s)
48+
s += s
49+
n = len(s)
50+
s1 = ""
51+
s2 = ""
52+
for i in range(n):
53+
s1 += '1' if i % 2 == 0 else '0'
54+
s2 += '1' if i % 2 == 1 else '0'
55+
56+
res = k
57+
count1 = 0
58+
count2 = 0
59+
for i in range(n):
60+
if i >= k:
61+
if s[i - k] != s1[i - k]:
62+
count1 -= 1
63+
if s[i - k] != s2[i - k]:
64+
count2 -= 1
65+
66+
if s[i] != s1[i]:
67+
count1 += 1
68+
if s[i] != s2[i]:
69+
count2 += 1
70+
71+
if i >= k - 1:
72+
res = min(res, count1, count2)
73+
74+
return res
75+
76+
class Solution:
77+
def minFlips(self, s: str) -> int:
78+
n = len(s)
79+
counter = [0, 0]
80+
outer = (n + 1) // 2
81+
inner = n - outer
82+
83+
res = n
84+
for i in range(2 * n):
85+
if i >= n:
86+
counter[(i - n) & 1] -= int(s[i - n])
87+
88+
counter[i & 1] += int(s[i % n])
89+
90+
if i >= n - 1:
91+
k = (i - n + 1) % 2
92+
count = min(outer - counter[k] + counter[1 - k], counter[k] + inner - counter[1 - k])
93+
res = min(res, count)
94+
95+
return res

0 commit comments

Comments
 (0)