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 }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
other
/
KeyFormat.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
70 lines (44 loc) · 1.71 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
other
/
KeyFormat.java
Copy path
Top
File metadata and controls
Code
Blame
70 lines (44 loc) · 1.71 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
package other;
/**
* @Author: Wenhang Chen
* @Description:有一个密钥字符串 S ,只包含字母,数字以及 '-'(破折号)。其中, N 个 '-' 将字符串分成了 N+1 组。
* <p>
* 给你一个数字 K,请你重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字符;而第一个分组中,至少要包含 1 个字符。两个分组之间需要用 '-'(破折号)隔开,并且将所有的小写字母转换为大写字母。
* <p>
* 给定非空字符串 S 和数字 K,按照上面描述的规则进行格式化。
* <p>
*
* <p>
* 示例 1:
* <p>
* 输入:S = "5F3Z-2e-9-w", K = 4
* 输出:"5F3Z-2E9W"
* 解释:字符串 S 被分成了两个部分,每部分 4 个字符;
* 注意,两个额外的破折号需要删掉。
* 示例 2:
* <p>
* 输入:S = "2-5g-3-J", K = 2
* 输出:"2-5G-3J"
* 解释:字符串 S 被分成了 3 个部分,按照前面的规则描述,第一部分的字符可以少于给定的数量,其余部分皆为 2 个字符。
*
* <p>
* 提示:
* <p>
* S 的长度可能很长,请按需分配大小。K 为正整数。
* S 只包含字母数字(a-z,A-Z,0-9)以及破折号'-'
* S 非空
* @Date: Created in 20:57 6/17/2020
* @Modified by:
*/
public class KeyFormat {
public String licenseKeyFormatting(String S, int K) {
S = S.replace("-", "");
S = S.toUpperCase();
int stLen = S.length() % K == 0 ? K : S.length() - S.length() / K * K;
StringBuffer str = new StringBuffer(S);
for (int i = stLen; i < str.length(); i = i + K + 1) {
str = str.insert(i, "-");
}
return str.toString();
}
}
You can’t perform that action at this time.