Problem
Our normal words do not have more than two consecutive letters. If there are three or more consecutive letters, this is a tics. Now give a word, from left to right, to find out the starting point and ending point of all tics.
ExampleGiven str = "whaaaaatttsup", return [[2,6],[7,9]].
Explanation:
"aaaa" and "ttt" are twitching letters, and output their starting and ending points.
Given str = "whooooisssbesssst", return [[2,5],[7,9],[12,15]].
Explanation:
"ooo", "sss" and "ssss" are twitching letters, and output their starting and ending points.
public class Solution { /** * @param str: the origin string * @return: the start and end of every twitch words */ public int[][] twitchWords(String str) { //set two boundaries, one pre value, only move the right boundary List> res = new ArrayList<>(); int l = 0, r = 0; char[] strs = str.toCharArray(); char pre = strs[0]; for (int i = 0; i < strs.length; i++) { List
cur = new ArrayList<>(); if (i != 0 && strs[i] != pre) { if (r-l >= 2) { cur.add(l); cur.add(r); res.add(cur); } l = i; r = i; pre = strs[i]; } else if (strs[i] == pre) { r = i; } } //when the last three chars are twitch if (r-l >= 2) { List cur = new ArrayList<>(); cur.add(l); cur.add(r); res.add(cur); } int[][] ans = new int[res.size()][2]; for (int i = 0; i < res.size(); i++) { ans[i][0] = res.get(i).get(0); ans[i][1] = res.get(i).get(1); } return ans; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71131.html
LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...
Problem Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example, great acting skills a...
Problem Given a dictionary, find all of the longest words in the dictionary. Example Given { dog, google, facebook, internationalization, blabla } the longest words are(is) [internationaliz...
Problem Given a non-negative integer n, print the number in words. Example Given n = 125 Return one hundred twenty five Solution class Solution { private String[] belowTen = new String[] {, One, T...
摘要:使用,利用其按層次操作的性質(zhì),可以得到最優(yōu)解。這樣可以保證這一層被完全遍歷。每次循環(huán)取出的元素存為新的字符串。一旦找到和相同的字符串,就返回轉(zhuǎn)換序列長度操作層數(shù),即。 Problem Given two words (start and end), and a dictionary, find the length of shortest transformation sequence...
閱讀 1904·2021-11-22 09:34
閱讀 3042·2021-09-28 09:35
閱讀 13499·2021-09-09 11:34
閱讀 3608·2019-08-29 16:25
閱讀 2840·2019-08-29 15:23
閱讀 2051·2019-08-28 17:55
閱讀 2440·2019-08-26 17:04
閱讀 3056·2019-08-26 12:21