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:

  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.
  1. /**
  2. * @param {string} S
  3. * @param {number} K
  4. * @return {string}
  5. */
  6. var licenseKeyFormatting = function (S, K) {
  7. let res = "";
  8. let str = S.replace(/-/g, "");
  9. let groupLength = 0;
  10. for (let i = str.length - 1; i >= 0; i--) {
  11. res = str[i].toUpperCase() + res;
  12. if (++groupLength == K && i != 0) {
  13. res = "-" + res;
  14. groupLength = 0;
  15. }
  16. }
  17. return res;
  18. };
  19. let s = "2-5g-3-J";
  20. let k = 2;
  21. let res = licenseKeyFormatting(s, k);
  22. console.log(res);

来自为知笔记(Wiz)

时间: 2024-10-13 18:09:09

482. License Key Formatting 格式化激活码的相关文章

482. License Key Formatting - LeetCode

Question 482.?License Key Formatting Solution 思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-就忽略,如果遍历了k个字符(排除-)就追加一个-. Java实现1:insert版(StringBuilder的append()与insert()效率比较) public String licenseKeyFormatting(String S, int K) { StringBuilder sb = new StringB

【leetcode】482. License Key Formatting

problem 482. License Key Formatting 参考 1. Leetcode_482. License Key Formatting; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10592187.html

[LeetCode] 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 ex

leetcode 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 ex

LeetCode_482. License Key Formatting

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

[LeetCode] 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 ex

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

这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字母数字字符和短划线.该字符串被N个破折号分成N + 1个组. 给定数字K,我们希望重新格式化字符串,使得每个组包含正好的K个字符,但第一个组可能比K短,但仍然必须包含至少一个字符.此外,必须在两个组之间插入短划线,并且所有小写字母都应转换为大写.给定非空字符串S和数字K,根据上述规则格式化字符串.例

PyCharm License Activation激活码失效问题的解决方法

自己的小Python项目好几天没有写了,今天打开PyCharm准备继续写,突然发现之前的激活码被取消不能用了,本来激情满满的准备干活啦!之前搜的这个激活码本来说的是可以用到18年1月份的呢,哎-心情是崩溃的--如下图所示:   在经过一顿搜索挣扎之后,笔者终于找到解决方法,血条瞬间满了,满血复活有没有,开开心心的开始敲代码:  解决方法是在下面的网站找到的,我只是个搬运工,来具体写一下步骤,解决一下网站中网友的问题,网址如下:http://idea.lanyus.com/  很多网友提问说,用了

Microsoft office 2019 永久密匙 密钥 key激活 激活码 office2019专业增强版 永久激活工具 序列号 官方正版

office2019需要激活码才能正常使用,而且office2019激活密钥价格不菲.目前网上已经有office2019大客户批量授权版的永久激活密钥,一个key可以激活一百万台机器的office2019.下面小编为大家整理分享office2019专业增强版产品密钥,绝对能永久激活office2019. 注意!注意!注意!最新版的激活方式在文章末尾处! 一.Office 2019专业增强版大客户版MAK永久激活密钥: Office 2019 ProPlus2019XC2RVL MAKC2R: [