{ "cells": [ { "cell_type": "markdown", "source": [ "In a string `s` of lowercase letters, these letters form consecutive groups of\n", "the same character.\n", "\n", "For example, a string like `s = \"abbxxxxzyy\"` has the groups `\"a\"`, `\"bb\"`,\n", "`\"xxxx\"`, `\"z\"`, and `\"yy\"`.\n", "\n", "A group is identified by an interval `[start, end]`, where `start` and `end`\n", "denote the start and end indices (inclusive) of the group. In the above\n", "example, `\"xxxx\"` has the interval `[3,6]`.\n", "\n", "A group is considered **large** if it has 3 or more characters.\n", "\n", "Return _the intervals of every **large** group sorted in **increasing order\n", "by start index**_.\n", "\n", "\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input: s = \"abbxxxxzzy\"\n", " Output: [[3,6]]\n", " **Explanation** : \"xxxx\" is the only large group with start index 3 and end index 6.\n", "\n", "\n", "**Example 2:**\n", "\n", "\n", "\n", " Input: s = \"abc\"\n", " Output: []\n", " **Explanation** : We have groups \"a\", \"b\", and \"c\", none of which are large groups.\n", "\n", "\n", "**Example 3:**\n", "\n", "\n", "\n", " Input: s = \"abcdddeeeeaabbbcd\"\n", " Output: [[3,5],[6,9],[12,14]]\n", " **Explanation** : The large groups are \"ddd\", \"eeee\", and \"bbb\".\n", "\n", "\n", "**Example 4:**\n", "\n", "\n", "\n", " Input: s = \"aba\"\n", " Output: []\n", "\n", "\n", "\n", "\n", "**Constraints:**\n", "\n", " * `1 <= s.length <= 1000`\n", " * `s` contains lower-case English letters only." ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "large_group_positions (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function large_group_positions(s::String)::Vector{Vector{Int}}\n", " ch = s[1]\n", " bg = 1\n", " res = Vector{Int}[]\n", " for i in 2:length(s)\n", " if s[i] != ch\n", " if i - bg > 2\n", " push!(res, [bg - 1, i - 2]) #1-index to 0-index\n", " end\n", " ch = s[i]\n", " bg = i\n", " end\n", " end\n", " 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 }