Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
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
{{ message }}
AhkunTa
Arithmetic
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Arithmetic
/
LeetCode
/
1415_javascript.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
73 lines (51 loc) · 1.89 KB
master
Breadcrumbs
Arithmetic
/
LeetCode
/
1415_javascript.js
Copy path
Top
File metadata and controls
Code
Blame
73 lines (51 loc) · 1.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 一个 「开心字符串」定义为:
// 仅包含小写字母 ['a', 'b', 'c'].
// 对所有在 1 到 s.length - 1 之间的 i ,满足 s[i] != s[i + 1] (字符串的下标从 1 开始)。
// 比方说,字符串 "abc","ac","b" 和 "abcbabcbcb" 都是开心字符串,但是 "aa","baa" 和 "ababbc" 都不是开心字符串。
// 给你两个整数 n 和 k ,你需要将长度为 n 的所有开心字符串按字典序排序。
// 请你返回排序后的第 k 个开心字符串,如果长度为 n 的开心字符串少于 k 个,那么请你返回 空字符串 。
//
// 示例 1:
// 输入:n = 1, k = 3
// 输出:"c"
// 解释:列表 ["a", "b", "c"] 包含了所有长度为 1 的开心字符串。按照字典序排序后第三个字符串为 "c" 。
// 示例 2:
// 输入:n = 1, k = 4
// 输出:""
// 解释:长度为 1 的开心字符串只有 3 个。
// 示例 3:
// 输入:n = 3, k = 9
// 输出:"cab"
// 解释:长度为 3 的开心字符串总共有 12 个 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"] 。第 9 个字符串为 "cab"
// 示例 4:
// 输入:n = 2, k = 7
// 输出:""
// 示例 5:
// 输入:n = 10, k = 100
// 输出:"abacbabacb"
//
// 提示:
// 1 <= n <= 10
// 1 <= k <= 100
/**
* @param {number} n
* @param {number} k
* @return {string}
*/
var getHappyString = function(n, k) {
let res = [];
let arr = ['a', 'b', 'c'];
let dfs = (tempArray,index) =>{
if(tempArray.length == n){
res.push(tempArray.join(''));
return;
}
for(let i=0; i<3; i++){
// 经典回溯 主要就在这里的剪枝
if(index > 0 && arr[i] == tempArray[index-1]) continue;
dfs([...tempArray, arr[i]], index+1);
}
}
dfs([],0);
return res[k-1] ? res[k-1] : "";
};
You can’t perform that action at this time.