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 乙级 1071.小赌怡情 C++/Java
题目来源 常言道“小赌怡情”.这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算机给出第二个数.若玩家猜对了,则系统奖励玩 ...
- python 获取当前,上级,上上级路径
import os print '***获取当前目录***' print os.getcwd() print os.path.abspath(os.path.dirname(__file__)) pr ...
- 解决关于VC++ 6.0打开文件时,程序停止的问题
不少boys和girls安装VC++ 6.0英文版后,开始学习C++语言,但是使用软件的过程中,点击“打开”时,就会出现程序进程错误,崩溃的事儿,很是郁闷.最后直接一个对话框如下: 并且vc6.0直接 ...
- MongoDB中4种日志的详细介绍
前言 任何一种数据库都有各种各样的日志,MongoDB也不例外.MongoDB中有4种日志,分别是系统日志.Journal日志.oplog主从日志.慢查询日志等.这些日志记录着MongoDB数据库不同 ...
- 使用openrc 管理容器中的服务
对于后台任务一般是不建议在容器中运行的,但是如果我们为了简化应用的部署,可能会使用后台任务进行服务的管理,类似的 工具很多,supervisor,systemd , init.d 同时对于docker ...
- pycharm中使用2to3
https://www.jianshu.com/p/abbb005ba002 可用
- spl_autoload_register 和 __autoload()魔术方法
在 PHP 5.3 之前,__autoload 函数抛出的异常不能被 catch 语句块捕获并会导致一个致命错误(Fatal Error). 尽管 __autoload() 函数也能自动加载类和接口 ...
- 用Matlab证明三维勾股定理
证明代码: syms a b c ; ab=sqrt(a^+b^); bc=sqrt(c^+b^); ca=sqrt(c^+a^); p=(ab+bc+ca)/; s1=(p*(p-ab)*(p-bc ...
- A~G)C004
AGC004 A Divide a Cuboi 我不会,谁能教教我/kk https://agc004.contest.atcoder.jp/submissions/7919351 B Colorfu ...
- 【POJ3126】Prime Path
本题传送门 本题知识点:宽度优先搜索 题意很简单.要找一个质数变到另一个质数的最少步数,两个质数都是4位数,变的时候只能变其中一位,变了的数也仍是质数. 思路也很简单,对每一位数进行修改,如果修改后的 ...