In a string S of lowercase letters, these letters form consecutive groups of the same character.

For example, a string like S = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z" and "yy".

Call a group large if it has 3 or more characters.  We would like the starting and ending positions of every large group.

The final answer should be in lexicographic order.

Example 1:

Input: "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.

Example 2:

Input: "abc"
Output: []
Explanation: We have "a","b" and "c" but no large group.

Example 3:

Input: "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]

较大分组的位置

关键位置在前后元素不同时的处理,索引需要重置,并判断重复次数.字符串末尾增加一个#字符是为了处理末尾满足重复次数大于三的情况.

class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
last_item = None
left_index = -1
nums_list = []
for i, item in enumerate(S + '#'):
if last_item != item:
if i - left_index > 2:
nums_list.append([left_index, i - 1])
left_index = i
last_item = item
return nums_list

830. Positions of Large Groups的更多相关文章

  1. 【Leetcode_easy】830. Positions of Large Groups

    problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...

  2. 830. Positions of Large Groups - LeetCode

    Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...

  3. 830. Positions of Large Groups@python

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  4. [LeetCode&Python] Problem 830. Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  5. 【LeetCode】830. Positions of Large Groups 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. Positions of Large Groups

    Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...

  7. [LeetCode] Positions of Large Groups 大群组的位置

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  8. [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  9. [LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

随机推荐

  1. IntelliJ IDEA 编译代码报错 找不到符号 符号: 找不到符号包 包

    在使用IDEA的时候,经常出现过找不到包或者找不到符号的情况,可以尝试以下几种方式来解决 1.如果项目使用的是Maven可以使用Maven-Reimport 2.还可以 Invalidate and ...

  2. 1.App爬取相关库的安装(安装Charles及手机端证书安装配置)

    一.官网下载Charles安装包: https://www.charlesproxy.com/download1.下载对应版本 我这里下载的是 win 64 bit (下载完解压,双击打开charle ...

  3. 《算法》第四章部分程序 part 9

    ▶ 书中第四章部分程序,包括在加上自己补充的代码,两种拓扑排序的方法 ● 拓扑排序 1 package package01; import edu.princeton.cs.algs4.Digraph ...

  4. mybatis二(参数处理和map封装及自定义resultMap)

    .单个参数 mybatis不会做特殊处理. #{参数名/任意名}:取出参数值. .多个参数 mybatis会做特殊处理. 多个参数会被封装成 一个map. key:param1...paramN,或者 ...

  5. windows server 域分发与分配软件

    参考网站:https://blog.csdn.net/southwind0/article/details/80734508 1.win 2008创建域 https://jingyan.baidu.c ...

  6. MySQL 中 mysqld_safe 与 mysqld 区别,以及 mysqld_safe 的使用介绍

    [mysqld_safe 与 mysqld 区别] 直接运行mysqld程序来启动MySQL服务的方法很少见 mysqld_safe脚本会在启动MySQL服务器后继续监控其运行情况,并在其死机时重新启 ...

  7. vue语法小练习

    实现功能:新增/删除 学生 <html> <head> <script src="https://cdn.staticfile.org/vue/2.2.2/vu ...

  8. ABAP-container拆分

    1.界面 2.代码 *&---------------------------------------------------------------------* *& Report ...

  9. cordova-config.xml 配置记录

    <?xml version='1.0' encoding='utf-8'?> <widget id="come.gs.webapp1" version=" ...

  10. JAVA面试中的陷阱

    第一,谈谈final, finally, finalize的区别.最常被问到. 第二,Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以impl ...