expressive
题目描述
Sometimes people repeat letters to represent extra feeling, such as “hello” -> “heeellooo”, “hi” -> “hiiii”. Here, we have groups, of adjacent letters that are all the same character, and adjacent characters to the group are different. A group is extended if that group is length 3 or more, so “e” and “o” would be extended in the first example, and “i” would be extended in the second example. As another example, the groups of “abbcccaaaa” would be “a”, “bb”, “ccc”, and “aaaa”; and “ccc” and “aaaa” are the extended groups of that string.
For some given string S, a query word is stretchy if it can be made to be equal to S by extending some groups. Formally, we are allowed to repeatedly choose a group (as defined above) of characters c, and add some number of the same character c to it so that the length of the group is 3 or more. Note that we cannot extend a group of size one like “h” to a group of size two like “hh” - all extensions must leave the group extended - ie., at least 3 characters long.
Given a list of query words, return the number of words that are stretchy.
Example:
Input:
S = “heeellooo”
words = [“hello”, “hi”, “helo”]
Output: 1
Explanation:
We can extend “e” and “o” in the word “hello” to get “heeellooo”.
We can’t extend “helo” to get “heeellooo” because the group “ll” is not extended.
Notes:
0 <= len(S) <= 100.
0 <= len(words) <= 100.
0 <= len(words[i]) <= 100.
S and all words in words consist only of lowercase letters
解题思路
class Solution {
public int expressiveWords(String S, String[] words) {
int count = 0;
for (String word : words) {
if (isStretchy(word, S)) {
count++;
}
}
return count;
}
private boolean isStretchy(String original, String stretched) {// hello heeellooo
char[] origChars = original.toCharArray(); //origChars: h e l l o
char[] stretchedChars = stretched.toCharArray(); //stretched: h e e e l l o o o
int i = 0;
int j = 0;
while (i < origChars.length && j < stretchedChars.length) {
char currentChar = origChars[i]; //h, //e
if (currentChar != stretchedChars[j]) {//h==h // e==e
return false;
}
int origCharCount = 0;//统计当前字符次数
int stretchCharCount = 0;//统计当前字符在S={h e e e l l o o o}中次数
while (i < origChars.length && origChars[i] == currentChar) {
origCharCount++; //1 //1
i++;//i=1,指向 h e l l o 中的 e //i=2,指向 l
}
while (j < stretchedChars.length && stretchedChars[j] == currentChar) {
stretchCharCount++; //1 //3
j++;//j=1,指向 stretched: h e e e l l o o o中的 e //j=4,指向l
}
if (stretchCharCount > origCharCount && stretchCharCount < 3) {
return false;
}
if (origCharCount > stretchCharCount) {
return false;
}
}
return i == origChars.length && j == stretchedChars.length; //i==5,j==9 hello可以扩展为heeellooo
}
/* public static void main(String[] args) {
Solution solution=new Solution();
String S="heeellooo";
String[]words={"hello","hi","helo"};
int output=solution.expressiveWords(S,words);
System.out.println(output);
}*/
}
相关阅读
“Adwords竞价很赚钱!暴利赚钱!”网络上各种不同的培训都在说这句话,但其实任何事物都不是这么简单,任何一门赚钱的技巧都
大多数PPC经理都会同意,Google AdWords的网站链接对PPC账户来说是强大的优化工具。在我们的Google AdWords代表的鼓励下,我花了更
许多博客上的朋友及我的一些客户都会经常问到我一个问题,到底是百度竞价排名效果好,还是Google Adwords广告效果要好? 如果从平台的
Google Play Store启用AdWords搜索广告
Google Play Store已经覆盖了190多个国家/地区的超过10亿Android设备用户。今年2月份开始,Google Play Store就开始小范围测试搜索
Meta代码优化(二)Keywords和Description优化标准
大家好,我是酿蜜。前两天给大家分享了Meta代码优化(一)--title优化的优化标准,现在继续给大家分享下Meta代码优化(二)即Keywords和Descr