482. License Key Formatting

Easy

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:

  1. The length of string S will not exceed 12,000, and K is a positive integer.
  2. String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
  3. 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的更多相关文章

  1. 【leetcode】482. License Key Formatting

    problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...

  2. 482. License Key Formatting - LeetCode

    Question 482. License Key Formatting Solution 思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-就忽略,如果遍历了k个字符 ...

  3. [LeetCode] License Key Formatting 注册码格式化

    Now you are given a string S, which represents a software license key which we would like to format. ...

  4. [Swift]LeetCode482. 密钥格式化 | License Key Formatting

    You are given a license key represented as a string S which consists only alphanumeric character and ...

  5. [LeetCode] 482. License Key Formatting 注册码格式化

    You are given a license key represented as a string S which consists only alphanumeric character and ...

  6. 【LeetCode】482. License Key Formatting 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode算法题-License Key Formatting(Java实现)

    这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...

  8. 482. License Key Formatting

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 482 License Key Formatting 注册码格式化

    详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...

随机推荐

  1. ssh ssm ssi 服务器平台架构

    1.spring + springmvc + mybatis 使用maven构建,数据库是oracle,主要是温习mybatis配置以及与spring集成.这种框架目前是使用最广泛的,这里就不多说 2 ...

  2. [React] Fetch Data with React Suspense

    Let's get started with the simplest version of data fetching with React Suspense. It may feel a litt ...

  3. CentOS7配置VIP

    CentOS7 两台做vip飘逸,实现虚拟ip的跳转 两台机器 首先下载ipvsadm 和 keepalived yum -y install ipvsadm keepalived vim /etc/ ...

  4. 浏览器正在等待locatehost的响应

    1.问题描述 在进行了几次增删改查操作后,浏览器显示浏览器正在等待locatehost的响应: 2.错误原因: 对数据库为关闭相应的资源. 3.解决方案: 关闭JDBC开启的资源:

  5. .net core 从 ActionFilterAttribute 获取Request.Body 的正确方式

    由于 ModelBinding在动作过滤器之前运行,直接使用 context.ActionArguments["parameter"]  获取模型对象 This article s ...

  6. 一篇文章把你带入到JavaScript中的闭包与高级函数

    在JavaScript中,函数是一等公民.JavaScript是一门面向对象的编程语言,但是同时也有很多函数式编程的特性,如Lambda表达式,闭包,高阶函数等,函数式编程时一种编程范式. funct ...

  7. UDP丢包原因总结

    丢包检查方法 给每个UDP包编号,对比收发端的接收到的包.对于UDP协议层上的包,例如RTP包,可以从RTP包中读出包的序列号进行判断. 抓包.发送端和接收端分别抓包.linux下可以使用tcpdum ...

  8. RSA加密和数字签名在Java中常见应用【原创】

    相关术语解释: RSA,参考: https://en.wikipedia.org/wiki/RSA_(cryptosystem) 非对称加密算法 ,参考:https://baike.baidu.com ...

  9. 前端知识点回顾之重点篇——CSS中flex布局

    flex布局 来源: http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool 采用 Flex 布局的元素 ...

  10. 019 spring social

    1.原理 2. 3. 4.