当前位置:网站首页>Leetcode- key formatting - simple
Leetcode- key formatting - simple
2022-06-13 05:49:00 【AnWenRen】
title :482 Key format - Simple
subject
There is a key string S , Letters only , Figures and ‘-’( Dashes ). among , N individual ‘-’ Divide the string into N+1 Group .
Here's a number K, Please reformat the string , Make each group just contain K Characters . Specially , The number of characters in the first group must be less than or equal to K, But at least include 1 Characters . Between two groups, you need to use ‘-’( Dashes ) separate , And convert all lowercase letters to uppercase letters .
Given a non empty string S And number K, Format according to the rules described above .
Example 1
Input :S = "5F3Z-2e-9-w", K = 4
Output :"5F3Z-2E9W"
explain : character string S It's divided into two parts , Each part 4 Characters ;
Be careful , Two extra dashes need to be deleted .
Example 2
Input :S = "2-5g-3-J", K = 2
Output :"2-5G-3J"
explain : character string S Divided into 3 Parts of , Follow the previous rules to describe , The characters in the first part can be less than the given number , The rest are 2 Characters .
Tips
- S The length of may be very long , Please allocate the size as needed .K As a positive integer .
- S Only alphanumeric (a-z,A-Z,0-9) And dashes ’-’
- S Non empty
Code Java
public String licenseKeyFormatting(String s, int k) {
StringBuilder sb = new StringBuilder(s);
StringBuilder result = new StringBuilder();
int count = 0;
for (int i = sb.length() - 1; i >= 0; i --) {
if (sb.charAt(i) != '-') {
if (count % k == 0 && count != 0){
result.append('-');
}
if (sb.charAt(i) >= 'a' && sb.charAt(i) <= 'z')
result.append((char) (sb.charAt(i) - 32));
else
result.append(sb.charAt(i));
count ++;
}
}
return result.reverse().toString();
}
边栏推荐
- Randomly fetch data from the list
- What happens when the MySQL union index ABC encounters a "comparison operator"?
- Leetcode- reverse vowels in string - simple
- Compilation croisée helloworld avec cmake
- 890. Find and Replace Pattern
- 2020 personal annual summary
- arrayList && linkedList
- How to Algorithm Evaluation Methods
- Leetcode- intersection of two arrays ii- simple
- Web site learning and sorting
猜你喜欢
Nacos series registry principle and source code analysis
为什么那么多人讨厌A-Spice
Sentinel series integrates Nacos and realizes dynamic flow control
MySQL performs an inner join on query. The query result is incorrect because the associated fields have different field types.
Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience
Web site learning and sorting
Quartz basic use
Building a stand-alone version of Nacos series
MySQL transactions and foreign keys
AUTOSAR实战教程pdf版
随机推荐
[automated test] cypress manual
Integer tips
Comment procéder à l'évaluation des algorithmes
Problems encountered in the use of PgSQL
Solutions to conflicts between xampp and VMware port 443
Current limiting and fusing of gateway gateway in Spirng cloud
Etcd fast cluster building
Sentinel series integrates Nacos and realizes dynamic flow control
One of PowerShell optimizations: prompt beautification
Information collection for network security (2)
Tongweb crawl performance log script
About Evaluation Metrics
Difference between deviation and variance in deep learning
Application virtual directory static resource configuration on tongweb
软件测试——接口常见问题汇总
Four shardingsphere JDBC sharding strategies
Some methods of string
Quartz database storage
Solve the problem of garbled code in the MySQL execution SQL script database in docker (no need to rebuild the container)
= = relation between int and integer