当前位置:网站首页>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();
}
边栏推荐
- Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
- No assessment summary
- Sentinel series integrates Nacos and realizes dynamic flow control
- Tongweb crawl performance log script
- How to Algorithm Evaluation Methods
- Leetcode judge subsequence simple
- 计算两个时间相差的天数(支持跨月、跨年)
- Vagrant virtual machine installation, disk expansion and LAN access tutorial
- A fast week
- Pychart professional edition's solution to SQL script error reporting
猜你喜欢

零拷贝技术

Four shardingsphere JDBC sharding strategies

Ffmpeg download suffix is Video files for m3u8

Etcd understanding of microservice architecture

ffmpeg 下载后缀为.m3u8的视频文件

1 Introduction to drools rule engine (usage scenarios and advantages)

Sentinel series introduction to service flow restriction

Django uploads local binaries to the database filefield field

2 first experience of drools

Tongweb card, tongweb card, tongweb card
随机推荐
OpenGL Mosaic (8)
Fast power code
Leetcode- reverse vowels in string - simple
Django uploads local binaries to the database filefield field
Timeout thread log for tongweb
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
10 signalstartevent and signalcatchingevent of flowable signal events
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
Service architecture diagram of Nacos series
零拷贝技术
20 flowable container (event sub process, things, sub process, pool and pool)
No assessment summary
Error: unmapped character encoding GBK
2021.9.29学习日志-MIME类型
Browser screenshot method (long screenshot, node screenshot, designated area screenshot)
JNDI configuration for tongweb7
Windbos run command set
Summary of the 11th week of sophomore year
Quartz database storage
1 Introduction to drools rule engine (usage scenarios and advantages)