★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9799232.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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 = 4Output: "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 = 2Output: "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.

给定一个密钥字符串S,只包含字母,数字以及 '-'(破折号)。N 个 '-' 将字符串分成了 N+1 组。给定一个数字 K,重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字符,第一个分组至少要包含 1 个字符。两个分组之间用 '-'(破折号)隔开,并且将所有的小写字母转换为大写字母。

给定非空字符串 S 和数字 K,按照上面描述的规则进行格式化。

示例 1:

输入:S = "5F3Z-2e-9-w", K = 4输出:"5F3Z-2E9W"解释:字符串 S 被分成了两个部分,每部分 4 个字符;注意,两个额外的破折号需要删掉。

示例 2:

输入:S = "2-5g-3-J", K = 2输出:"2-5G-3J"解释:字符串 S 被分成了 3 个部分,按照前面的规则描述,第一部分的字符可以少于给定的数量,其余部分皆为 2 个字符。

提示:

  1. S 的长度不超过 12,000,K 为正整数
  2. S 只包含字母数字(a-z,A-Z,0-9)以及破折号'-'
  3. S 非空

 1 class Solution {
 2     func licenseKeyFormatting(_ S: String, _ K: Int) -> String {
 3         var res:[String] = [String]()
 4         for index in S.indices.reversed()
 5         {
 6             if S[index] != "-"
 7             {
 8                 if res.count % (K + 1) == K
 9                 {
10                     res.append("-")
11                 }
12                 res.append(String(S[index]))
13             }
14         }
15         //字符数组转字符串
16         var str:String = String(res.joined(separator: "").reversed())
17         return str.uppercased()
18     }
19 }


132ms

 1 class Solution {
 2     func licenseKeyFormatting(_ S: String, _ K: Int) -> String {
 3         var bufferS: String = ""
 4         var result: String = ""
 5
 6         if(S.count == 0) { return "" }
 7
 8         for c in S {
 9             if(String(c) != "-") {
10                 bufferS += (String(c)).uppercased()
11             }
12         }
13
14         var i: Int = bufferS.count % K == 0 ? K : bufferS.count % K
15         for c in bufferS {
16             if(i == 0) {
17                 result += "-"
18                 i = K
19             }
20             if(i > 0) {
21                 result += String(c)
22                 i -= 1
23             }
24         }
25
26         return result
27     }
28 }


220ms

 1 class Solution {
 2     func licenseKeyFormatting(_ S: String, _ K: Int) -> String {
 3         let s = S.replacingOccurrences(of: "-", with: "").uppercased()
 4         let chas = [Character](s)
 5
 6         var res = ""
 7         res.append(String(chas[..<(chas.count%K)]))
 8
 9         for i in stride(from: chas.count % K, to: chas.count, by: K) {
10             if !res.isEmpty {
11                 res.append("-")
12             }
13             res.append(String(chas[i..<(i+K)]))
14         }
15
16         return res
17     }
18 }


368ms

 1 class Solution {
 2     func licenseKeyFormatting(_ S: String, _ K: Int) -> String {
 3         var stringArray = S.split(separator: "-").joined(separator: "").map { String($0) }
 4
 5         var returnString: String = ""
 6
 7         while(!stringArray.isEmpty) {
 8             let subArray: String = Array(stringArray.suffix(K)).reduce("", +).uppercased()
 9             for _ in 0..<subArray.count {
10                 stringArray.removeLast()
11             }
12             returnString = stringArray.isEmpty ? "\(subArray)" + returnString : "-\(subArray)" + returnString
13
14         }
15
16         return returnString
17     }
18 }


1052ms

 1 class Solution {
 2     func licenseKeyFormatting(_ S: String, _ K: Int) -> String {
 3
 4         var n = 0
 5         for c in S {
 6             if c != "-" {
 7                 n += 1
 8             }
 9         }
10
11         var num_g = n / K
12         var first = K
13         if n % K > 0 {
14             first = n % K
15             num_g += 1
16         }
17
18         var res = ""
19         var temp = ""
20         var count_g = 0
21
22         for c in S {
23             if c != "-" {
24                 temp += String(c).uppercased()
25                 if (count_g == 0 && temp.count == first && count_g != num_g-1) || (count_g < num_g-1 && temp.count == K) {
26                     res += temp + "-"
27                     temp = ""
28                     count_g += 1
29                 } else if (count_g == num_g-1 && temp.count == K) || (count_g == 0 && temp.count == first) {
30                     res += temp
31                     temp = ""
32                     count_g += 1
33                 }
34             }
35         }
36
37         return res
38     }
39 }

转载于:https://www.cnblogs.com/strengthen/p/9799232.html

[Swift]LeetCode482. 密钥格式化 | License Key Formatting相关推荐

  1. Leet Code OJ 482. License Key Formatting [Difficulty: Medium]

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

  2. 482. License Key Formatting

    最后更新 一刷 11-Jan-2017 这个题是Ez难度的吧... 注意edge cases就行. 熟悉下Character的方程: Character.isDigit(c); Character.i ...

  3. phpstorm8 license key

    2019独角兽企业重金招聘Python工程师标准>>> phpstorm8破解及license key Learn Programming ===== LICENSE BEGIN = ...

  4. php设置key,phpstorm8 设置及license key

    phpstorm8 license key Learn Programming ===== LICENSE BEGIN ===== 63758-12042010 00000Ryqh0NCC73lpRm ...

  5. 482. 密钥格式化

    482. 密钥格式化 有一个密钥字符串 S ,只包含字母,数字以及 '-'(破折号).其中, N 个 '-' 将字符串分成了 N+1 组. 给你一个数字 K,请你重新格式化字符串,使每个分组恰好包含 ...

  6. SAP算号器 license key Developer Access Key 完美解决方案

    本文遵从 GNU GPL 版权协议,任何组织或个人在引用.转载或修改此文档时,敬请保留版权信息及注明出处. 警告:仅限IDES学习.研究使用.严禁商业使用.后果自负. 关健字:SAP license ...

  7. webstorm license key

    JetBrains WebStorm注册码 UserName: William License Key : ===== LICENSE BEGIN ===== 45550-12042010 00001 ...

  8. Bitdefender Total Security 2014 Free 6 Months 12 month License Key

    German Only – Bitdefender Total Security 2014 Free 6 Months Serial License Key http://www.bitdefende ...

  9. sap LICENSE KEY和 ACCESS KEY 破解

    经过很长时间的折腾终于搞定了ECC 6.0的LICENSE KEY和 ACCESS KEY .终于可以创建用户,开发程序了.... 谢谢 http://kemiya.net/forum.php 论坛的 ...

最新文章

  1. CVPR 2021| 基于深度图匹配的鲁棒点云配准框架
  2. j函数 判断以 什么开头
  3. 【OpenCV3】基于双目视觉的三维重建
  4. Java计算一段程序的运行时间
  5. Winfrom 弹出窗体位置设定
  6. Java图片处理(二)图片加水印
  7. linux删除第二次出现的字符,linux下 怎样删除文件名中包含特殊字符的文件
  8. python基础整理——ASCII码、Unicode、utf-8、gbk
  9. matlab dicom图像异常,用Matlab处理Dicom图像
  10. SQL Server内联表值函数
  11. linux-LINUX试题
  12. Hyperledger Fabric教程(11)-- 链码和背书策略
  13. 异步任务,HttpContext.Current为null解决办法
  14. 2022-2028全球放射性废物管理系统行业调研及趋势分析报告
  15. golang 时间格式转换汇总
  16. 元宇宙大火的“天时、地利、人和”
  17. 在Ubuntu上玩《口袋妖怪·叶绿》
  18. ​蔚来高速换电站,大家还没看懂的护城河
  19. pandas无法创建excel文件或者无法读取excel文件
  20. 《用户画像》:方法论与工程化解决方案

热门文章

  1. [资源]181个Python开源项目分享!
  2. 百度地图gif图标_华为手机误删照片怎么找回?手机怎么快速制作GIF动图
  3. 01-几种应用上下文区别
  4. 【SR汇总】基于深度学习方法
  5. Microsoft SQL Server 全角转半角函数
  6. JAVA 成员访问权限修饰符
  7. ASP.NET MVC5+EF6+EasyUI 后台管理系统(51)-系统升级
  8. avalon框架,简单的MVVM
  9. 【观点】从曾成杰案看民间金融的高风险与银行缺失的机制创新
  10. 66-Flutter移动电商实战-会员中心_编写ListTile的通用方法