You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
+
classSolution {
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
+
boolhelper(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)) {
# 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"
* 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: -
41
6
42
-
#include<vector>
43
-
#include<string>
44
-
#include<unordered_map>
45
-
#include<iostream>
46
-
usingnamespacestd;
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.
0 commit comments