首页 > 其他分享 >leetcode-482-easy

leetcode-482-easy

时间:2022-11-21 22:25:04浏览次数:30  
标签:characters string int into 482 result easy each leetcode

License Key Formatting

You are given a license key represented as a string s that consists of only alphanumeric characters and dashes. The string is separated into n + 1 groups by n dashes. You are also given an integer k.

We want to reformat the string s 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 you should convert all lowercase letters to uppercase.

Return the reformatted license key.

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.
Constraints:

1 <= s.length <= 105
s consists of English letters, digits, and dashes '-'.
1 <= k <= 104

思路一:预处理原字符串,去掉 -,然后从后面开始遍历,插入 -

public String licenseKeyFormatting(String s, int k) {
    if (s == null || s.isEmpty()) return "";

    StringBuilder result = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) != '-') {
            result.append(s.charAt(i));
        }
    }

    int idx = 0;
    for (int i = result.length() - 1; i > 0; i--) {
        idx++;
        if (idx % k == 0) {
            result.insert(i, '-');
        }
    }

    return result.toString().toUpperCase();
}

标签:characters,string,int,into,482,result,easy,each,leetcode
From: https://www.cnblogs.com/iyiluo/p/16913563.html

相关文章

  • LeetCode199. Binary Tree Right Side View
    题意实现二叉树的右视图方法BFS代码/***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*ri......
  • [oeasy]python0018_ ASCII_字符分布_数字_大小写字母_符号_黑暗森林
    ​ 打包和解包回忆上次内容decode 就是解码解码和编码可以转化encode编码decode解码互为逆过程大小写字母之间序号全都相差(​​32​​)​​10进......
  • 提醒:EasyHub有14个新套件等你使用
    EasyHub是优维打造的一个权威标准的资源共享平台!优维将多年积累沉淀下来的最佳实践经验变成用户触手可及的资源,立足于各种运维场景,打造了丰富的资源套餐,覆盖云原生/DevOps......
  • 使用easy excel进行简单的导入导出表格
    1.创建项目,导入easyexcel的依赖<!--easyexcel依赖--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3......
  • easyexcel 2.2.6 新版本以列表的形式读取 Excel
    使用步骤:注:讲述使用EasyExcel的读取Excel数据列表的案例,项目基于springboot+maven模式。1、引入EasyExcel依赖包,文章以easyexcel2.2.6为基础。<dependency><......
  • leetcode300
    最长递增子序列Category Difficulty Likes Dislikesalgorithms Medium(52.52%) 2879 -TagsCompanies给你一个整数数组nums,找到其中最长严格递增子序列的长度。子......
  • 使用EasyCV Mask2Former轻松实现图像分割
    简介: EasyCV可以轻松预测图像的分割谱以及训练定制化的分割模型。本文主要介绍如何使用EasyCV实现实例分割、全景分割和语义分割,及相关算法思想。作者:贺弘谦言......
  • leetcode1894
    找到需要补充粉笔的学生编号Category Difficulty Likes Dislikesalgorithms Medium(45.75%) 98 -TagsCompaniesUnknown一个班级里有n个学生,编号为0到n-1。......
  • leetcode刷题第四周
    栈和队列:容器适配器,不提供迭代器232、用栈实现队列classMyQueue{Stack<Integer>stack1=newStack<>();Stack<Integer>stack2=newStack<>();pub......
  • [LeetCode] 1320. Minimum Distance to Type a Word Using Two Fingers 二指输入的的
    Youhaveakeyboardlayoutasshownaboveinthe X-Y plane,whereeachEnglishuppercaseletterislocatedatsomecoordinate.Forexample,theletter 'A......