【每日一题Day38】LC809情感丰富的文字 | 双指针 模拟
创始人
2024-04-12 11:01:11
0

情感丰富的文字【LC809】

Sometimes people repeat letters to represent extra feeling. For example:

  • "hello" -> "heeellooo"
  • "hi" -> "hiiii"

In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".

You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more.

  • For example, starting with "hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has a size less than three. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If s = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = s.

Return the number of query strings that are stretchy.

原来来来外国人人人人都是这样说话话话话的吗吗吗吗
全校静默的第一天天天天

  • 思路:统计words中可以转化为s的个数,转化方式为当s[i,rs] == word[j,rw]时,可以扩展word中字符使其与s相等,条件为rs−i+1≥3rs-i+1\geq3rs−i+1≥3。因此遍历words中的每个word,设置两个指针分别指向sword的首部,如果字符相等,那么寻找该字符在sword中的右边界,并判断是否需要进行扩展,以及能否进行扩展;如果字符不相等,那么一定不能进行转化,直接break

    • rs−i+1>rw−j+1rs - i + 1 > rw - j + 1rs−i+1>rw−j+1时,需要进行转化
      • rs−i+1≥3rs-i+1\geq3rs−i+1≥3时,不可以进行转化
      • rs−i+1<3rs-i+1\lt3rs−i+1<3时,可以进行转化
    • rs−i+1
    • rs−i+1==rw−j+1rs - i + 1 == rw - j + 1rs−i+1==rw−j+1时,不需要进行转化,继续判断
  • 实现:

    当word的长度大于s的长度时,那么一定不可以进行转化

    class Solution {public int expressiveWords(String s, String[] words) {int len = s.length();int count = 0;for (String word : words){int lenWord = word.length();if (lenWord > len){break;}int i = 0, j = 0;while (i < len && j < lenWord && s.charAt(i) == word.charAt(j)){             int rightS = getRightBorder(s,i);int rightWord = getRightBorder(word,j);if (rightS - i + 1 < rightWord - j + 1){break;}else if (rightS - i + 1 > rightWord - j + 1 && rightS - i + 1 < 3){break;}// rightS - i + 1 == rightWord - j + 1 // rightS - i + 1 > rightWord - j + 1 && rightS - i + 1 >= 3i = rightS + 1;j = rightWord + 1;if (i == len && j == lenWord){count++;}           }}return count;}public int getRightBorder(String s,int left){int right = left;char c = s.charAt(left);while (right + 1 < s.length() && s.charAt(right + 1) == c){right++;}return right;}
    }
    

相关内容

热门资讯

个人销售住房增值税政策来了 12月29日,财政部、国家税务总局发布“关于个人销售住房增值税政策的公告”。 个人(不含个体工商...
南京拟修订法规加强历史文化名城... 原题:保护利用将设“正负面清单” 《南京市历史文化名城保护条例》将迎大修 南京市历史文化名城保护法规...
《深圳公益诉讼检察工作白皮书(... 深圳新闻网2025年12月31日讯(深圳特区报记者 上官文复 通讯员 李俞青)12月30日,深圳市人...
了解招生政策要选择官方渠道 石向阳 绘 日前,清华大学招生办公室发布声明称,有部分机构与个人冒用学校名义开展招生宣讲,散布不实招...
视频丨日学者:高市言行和政策令... 日本上智大学政治学教授中野晃一近日在接受总台记者采访时指出,高市早苗有关对华立场的言论将进一步破坏日...
10年受理公益诉讼案件线索超8... 深圳特区报讯(记者 上官文复 通讯员 李俞青)12月30日,深圳市人民检察院举办《深圳公益诉讼检察工...
日学者:高市言行和政策令日本面... 日本上智大学政治学教授中野晃一近日在接受总台记者采访时指出,高市早苗有关对华立场的言论将进一步破坏日...
袁家军、胡衡华、刘明胜、徐树彪... 据重庆日报消息,12月30日下午, 国家电投集团水电股份有限公司揭牌。重庆市委书记袁家军,市委副书记...
“3女带4孩续面”被改编成动画... 极目新闻记者 詹钘 近日,有网友发现,郑州续面事件已经被人改编成动画,在短视频平台和短剧平台播放。视...
征兵政策解读之二:征集条件篇 一、年龄条件 1.义务兵。男青年年满18 至22 周岁,普通高等学校本专科毕业生、符合毕业条件的毕业...