LeetCode_482. License Key Formatting
482. License Key Formatting
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.
Given a non-empty string S and a number K, format the string according to the rules described above.
Example 1:
Input: S = "5F3Z-2e-9-w", K = 4 Output: "5F3Z-2E9W" Explanation: The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
Example 2:
Input: S = "2-5g-3-J", K = 2 Output: "2-5G-3J" Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
Note:
- The length of string S will not exceed 12,000, and K is a positive integer.
- String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
- String S is non-empty.
package leetcode.easy;
public class LicenseKeyFormatting {
public String licenseKeyFormatting(String S, int K) {
StringBuffer sb = new StringBuffer();
int count = 0;
for (int i = S.length() - 1; i >= 0; i--) {
char c = S.charAt(i);
if (c != '-') {
if (count > 0 && count % K == 0) {
sb.append('-');
}
sb.append(c);
count++;
}
}
return sb.reverse().toString().toUpperCase();
}
@org.junit.Test
public void test() {
System.out.println(licenseKeyFormatting("5F3Z-2e-9-w", 4));
System.out.println(licenseKeyFormatting("2-5g-3-J", 2));
}
}
LeetCode_482. License Key Formatting的更多相关文章
- 【leetcode】482. License Key Formatting
problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...
- 482. License Key Formatting - LeetCode
Question 482. License Key Formatting Solution 思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-就忽略,如果遍历了k个字符 ...
- [LeetCode] License Key Formatting 注册码格式化
Now you are given a string S, which represents a software license key which we would like to format. ...
- [Swift]LeetCode482. 密钥格式化 | License Key Formatting
You are given a license key represented as a string S which consists only alphanumeric character and ...
- [LeetCode] 482. License Key Formatting 注册码格式化
You are given a license key represented as a string S which consists only alphanumeric character and ...
- 【LeetCode】482. License Key Formatting 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode算法题-License Key Formatting(Java实现)
这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...
- 482. License Key Formatting
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 482 License Key Formatting 注册码格式化
详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...
随机推荐
- PAT 乙级 1010.一元多项式求导 C++/Java
设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为nxn−1.) 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数).数字间以空格分隔. ...
- Jmeter 局域网的IP欺骗终极解决方案
ip欺骗是什么? ip欺骗就是模拟ip.什么意思呢,一个电脑就只有一个ip地址,当然如果有多块网卡的话,会有多个ip地址,一般服务器上有个网卡,咱们自己的电脑一般都只有一个ip地址,但是你做压测 ...
- php析构函数什么时候调用?
析构函数何时被调用 析构函数在下边3种情况时被调用: 对象生命周期结束,被销毁时: 主动调用delete :(推荐学习:PHP编程从入门到精通) 对象i是对象o的成员,o的析构函数被调用时,对象i的析 ...
- linux内核中的文件描述符(二)--socket和文件描述符
http://blog.csdn.net/ce123_zhouwei/article/details/8459730 Linux内核中的文件描述符(二)--socket和文件描述符 Kernel ve ...
- [POJ3468]关于整数的简单题 (你想要的)树状数组区间修改区间查询
#include <cstdio> #include <algorithm> #include <cstring> #include <cctype> ...
- spark学习收集
spark优化: http://www.cnblogs.com/hark0623/p/5533803.html 董西成学生写的经验分享(很详细很强大) spark官网 API http://spark ...
- Mysql配置C3P0
需要导入的包 c3p0-0.9.5.2.jar mchange-commons-0.2.15.jar mysql-connector.jar 1. 配置xml 创建c3p0-config.xml文件, ...
- 二分法递归版本(c++)
利用二分法求解在区间[0,π/2]上的根 #include<iostream> #include <cmath> using namespace std; double dic ...
- ImportError: DLL load failed while importing win32api: 找不到指定的模块。
这个是用pip install pywin32安装报的一个错误 据说直接使用pip install pypiwin32安装就不会有报错 但是遇到错误还是要尝试解决一下的 pip install pyw ...
- wordpress 本地环境安装
1. 下载xmapp 2. 安装mysql 3. 启动xmapp的数据库与Apache,通常无法启动原因. mac的先关闭自己的数据库,系统偏好设置-MySQL Apache的端口默认是80,一般会被 ...