Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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.