Skip to content

Commit d112e41

Browse files
committed
Feb-19
1 parent 6021a3c commit d112e41

9 files changed

+329
-237
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Large diffs are not rendered by default.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Tag: String, Backtracking
2+
// Time: O(NK)
3+
// Space: O(N + K)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/8OpQ-jZ73Hg
7+
8+
// A happy string is a string that:
9+
//
10+
// consists only of letters of the set ['a', 'b', 'c'].
11+
// s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).
12+
//
13+
// For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
14+
// Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.
15+
// Return the kth string of this list or return an empty string if there are less than k happy strings of length n.
16+
//  
17+
// Example 1:
18+
//
19+
// Input: n = 1, k = 3
20+
// Output: "c"
21+
// Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
22+
//
23+
// Example 2:
24+
//
25+
// Input: n = 1, k = 4
26+
// Output: ""
27+
// Explanation: There are only 3 happy strings of length 1.
28+
//
29+
// Example 3:
30+
//
31+
// Input: n = 3, k = 9
32+
// Output: "cab"
33+
// Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab"
34+
//
35+
//  
36+
// Constraints:
37+
//
38+
// 1 <= n <= 10
39+
// 1 <= k <= 100
40+
//
41+
//
42+
43+
class Solution {
44+
public:
45+
string getHappyString(int n, int k) {
46+
vector<char> letters = {'a', 'b', 'c'};
47+
vector<string> res;
48+
helper(letters, res, n, k, "", 0);
49+
return res.size() == k ? res.back() : "";
50+
}
51+
52+
bool helper(vector<char> &letters, vector<string> &res, int n, int k, string tmp, int i) {
53+
if (i == n) {
54+
res.push_back(tmp);
55+
return res.size() == k;
56+
}
57+
58+
for (auto &ch: letters) {
59+
if (i == 0 || ch != tmp.back()) {
60+
if (helper(letters, res, n, k, tmp + ch, i + 1)) {
61+
return true;
62+
}
63+
}
64+
65+
}
66+
return false;
67+
}
68+
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Tag: String, Backtracking
2+
# Time: O(NK)
3+
# Space: O(N + K)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/8OpQ-jZ73Hg
7+
8+
# A happy string is a string that:
9+
#
10+
# consists only of letters of the set ['a', 'b', 'c'].
11+
# s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).
12+
#
13+
# For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
14+
# Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.
15+
# Return the kth string of this list or return an empty string if there are less than k happy strings of length n.
16+
#  
17+
# Example 1:
18+
#
19+
# Input: n = 1, k = 3
20+
# Output: "c"
21+
# Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
22+
#
23+
# Example 2:
24+
#
25+
# Input: n = 1, k = 4
26+
# Output: ""
27+
# Explanation: There are only 3 happy strings of length 1.
28+
#
29+
# Example 3:
30+
#
31+
# Input: n = 3, k = 9
32+
# Output: "cab"
33+
# Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab"
34+
#
35+
#  
36+
# Constraints:
37+
#
38+
# 1 <= n <= 10
39+
# 1 <= k <= 100
40+
#
41+
#
42+
43+
class Solution:
44+
def getHappyString(self, n: int, k: int) -> str:
45+
letter = ['a', 'b', 'c']
46+
res = []
47+
self.helper(letter, n, k, 0, res, '')
48+
return res[-1] if len(res) == k else ''
49+
50+
51+
def helper(self, letter: list, n: int, k: int, i: int, res: list, tmp: str) -> bool:
52+
if i == n:
53+
res.append(tmp)
54+
return len(res) == k
55+
56+
for l in letter:
57+
if i == 0 or l != tmp[-1]:
58+
if self.helper(letter, n, k, i + 1, res, tmp + l):
59+
return True
60+
61+
return False
Lines changed: 82 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,94 @@
1-
/*
2-
* [30] Substring with Concatenation of All Words
3-
*
4-
* https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/
5-
*
6-
* algorithms
7-
* Hard (22.51%)
8-
* Total Accepted: 162.7K
9-
* Total Submissions: 656.8K
10-
* Testcase Example: '"barfoothefoobarman"\n["foo","bar"]'
11-
*
12-
* You are given a string, s, and a list of words, words, that are all of the
13-
* same length. Find all starting indices of substring(s) in s that is a
14-
* concatenation of each word in words exactly once and without any intervening
15-
* characters.
16-
*
17-
*
18-
*
19-
* Example 1:
20-
*
21-
*
22-
* Input:
23-
* ⁠ s = "barfoothefoobarman",
24-
* ⁠ words = ["foo","bar"]
25-
* Output: [0,9]
26-
* Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar"
27-
* respectively.
28-
* The output order does not matter, returning [9,0] is fine too.
29-
*
30-
*
31-
* Example 2:
32-
*
33-
*
34-
* Input:
35-
* ⁠ s = "wordgoodgoodgoodbestword",
36-
* ⁠ words = ["word","good","best","word"]
37-
* Output: []
38-
*
39-
*
40-
*/
1+
// Tag: Hash Table, String, Sliding Window
2+
// Time: O(N)
3+
// Space: O(K)
4+
// Ref: -
5+
// Note: -
416

42-
#include <vector>
43-
#include <string>
44-
#include <unordered_map>
45-
#include <iostream>
46-
using namespace std;
7+
// You are given a string s and an array of strings words. All the strings of words are of the same length.
8+
// A concatenated string is a string that exactly contains all the strings of any permutation of words concatenated.
9+
//
10+
// For example, if words = ["ab","cd","ef"], then "abcdef", "abefcd", "cdabef", "cdefab", "efabcd", and "efcdab" are all concatenated strings. "acdbef" is not a concatenated string because it is not the concatenation of any permutation of words.
11+
//
12+
// Return an array of the starting indices of all the concatenated substrings in s. You can return the answer in any order.
13+
//  
14+
// Example 1:
15+
//
16+
// Input: s = "barfoothefoobarman", words = ["foo","bar"]
17+
// Output: [0,9]
18+
// Explanation:
19+
// The substring starting at 0 is "barfoo". It is the concatenation of ["bar","foo"] which is a permutation of words.
20+
// The substring starting at 9 is "foobar". It is the concatenation of ["foo","bar"] which is a permutation of words.
21+
//
22+
// Example 2:
23+
//
24+
// Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
25+
// Output: []
26+
// Explanation:
27+
// There is no concatenated substring.
28+
//
29+
// Example 3:
30+
//
31+
// Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
32+
// Output: [6,9,12]
33+
// Explanation:
34+
// The substring starting at 6 is "foobarthe". It is the concatenation of ["foo","bar","the"].
35+
// The substring starting at 9 is "barthefoo". It is the concatenation of ["bar","the","foo"].
36+
// The substring starting at 12 is "thefoobar". It is the concatenation of ["the","foo","bar"].
37+
//
38+
//  
39+
// Constraints:
40+
//
41+
// 1 <= s.length <= 104
42+
// 1 <= words.length <= 5000
43+
// 1 <= words[i].length <= 30
44+
// s and words[i] consist of lowercase English letters.
45+
//
46+
//
4747

4848
class Solution {
4949
public:
5050
vector<int> findSubstring(string s, vector<string>& words) {
51-
vector<int> ans;
52-
if (s.size() == 0 || words.size() == 0 || words[0].size() == 0) return ans;
53-
54-
int num = (int)words.size();
55-
int word_len = (int)words[0].size();
56-
int substring_len = num * word_len;
57-
58-
unordered_map<string, int> word_count;
59-
for (int i = 0; i < words.size(); i++) {
60-
word_count[words[i]] += 1;
51+
int n = s.size();
52+
int k = words.size();
53+
int l = words[0].size();
54+
vector<int> res;
55+
unordered_map<string, int> counter;
56+
for (auto x: words) {
57+
counter[x] += 1;
6158
}
62-
63-
for (int i = 0; i < num - substring_len + 1; i++) {
64-
string test_string = s.substr(i, substring_len);
65-
unordered_map<string, int> test_count;
59+
60+
for (int index = 0; index < l; index++) {
61+
int left = index;
6662
int count = 0;
67-
for (int j = 0; j < substring_len; j += word_len) {
68-
string test_word = test_string.substr(j, word_len);
63+
int hit = 0;
64+
unordered_map<string, int> current_counter = counter;
65+
for (int i = index; i <= n - l; i += l) {
66+
string sub = s.substr(i, l);
67+
count += 1;
68+
if (current_counter.count(sub) > 0) {
69+
current_counter[sub] -= 1;
70+
if (current_counter[sub] >= 0) {
71+
hit += 1;
72+
}
73+
}
6974

70-
if (word_count.count(test_word) && test_count[test_word] < word_count[test_word]) {
71-
count++;
72-
test_count[test_word] += 1;
73-
} else {
74-
break;
75+
if (count == k) {
76+
if (hit == k) {
77+
res.push_back(left);
78+
}
79+
string pre = s.substr(left, l);
80+
if (current_counter.count(pre) > 0) {
81+
current_counter[pre] += 1;
82+
if (current_counter[pre] >= 1) {
83+
hit -= 1;
84+
}
85+
}
86+
count -= 1;
87+
left += l;
7588
}
7689
}
77-
78-
if (count == num) {
79-
ans.push_back(i);
80-
}
8190
}
82-
83-
return ans;
84-
}
85-
};
86-
87-
int main(int argc, char const *argv[])
88-
{
89-
string a[] = {"foo","bar"};
90-
vector<string> vec(a, a + 3);
91-
Solution s;
92-
vector<int> res = s.findSubstring("barfoothefoobarman", vec);
9391

94-
return 0;
95-
}
92+
return res;
93+
}
94+
};

0 commit comments

Comments
 (0)