{ "cells": [ { "cell_type": "markdown", "source": [ "You are given a license key represented as a string S which consists only\n", "alphanumeric character and dashes. The string is separated into N+1 groups by\n", "N dashes.\n", "\n", "Given a number K, we would want to reformat the strings such that each group\n", "contains _exactly_ K characters, except for the first group which could be\n", "shorter than K, but still must contain at least one character. Furthermore,\n", "there must be a dash inserted between two groups and all lowercase letters\n", "should be converted to uppercase.\n", "\n", "Given a non-empty string S and a number K, format the string according to the\n", "rules described above.\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input: S = \"5F3Z-2e-9-w\", K = 4\n", "\n", " Output: \"5F3Z-2E9W\"\n", "\n", " Explanation: The string S has been split into two parts, each part has 4 characters.\n", " Note that the two extra dashes are not needed and can be removed.\n", "\n", "\n", "**Example 2:**\n", "\n", "\n", "\n", " Input: S = \"2-5g-3-J\", K = 2\n", "\n", " Output: \"2-5G-3J\"\n", "\n", " 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.\n", "\n", "\n", "**Note:**\n", "\n", " 1. The length of string S will not exceed 12,000, and K is a positive integer.\n", " 2. String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).\n", " 3. String S is non-empty." ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "license_key_formatting (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function license_key_formatting(S::String, K::Int)\n", " res = \"\"\n", " cnt = 0\n", " for i in length(S):-1:1\n", " if cnt == K\n", " res *= '-'\n", " cnt = 0\n", " end\n", " if S[i] != '-'\n", " res *= uppercase(S[i])\n", " cnt += 1\n", " end\n", " end\n", " reverse(res)\n", "end\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }