当前位置: 首页 > news >正文

网站好坏标准seo网站诊断文档案例

网站好坏标准,seo网站诊断文档案例,有口碑的南通网站建设,万秀服务不错的seo推广目录 1.题目2.答案3.提交结果截图4.图解 链接: 串联所有单词的子串 1.题目 给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。 s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。 例如,如果 w…

在这里插入图片描述

目录

    • 1.题目
    • 2.答案
    • 3.提交结果截图
    • 4.图解

链接: 串联所有单词的子串

1.题目

给定一个字符串 s 和一个字符串数组 words words 中所有字符串 长度相同

s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。

  • 例如,如果 words = ["ab","cd","ef"], 那么 "abcdef""abefcd""cdabef""cdefab""efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。

返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。

示例 1:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。

示例 2:

输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。

示例 3:

输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]
解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。
子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。
子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。
子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。

提示:

  • 1 <= s.length <= 10^4
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 30
  • words[i]s 由小写英文字母组成

2.答案

class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> result = new ArrayList<>();int wordLength = words[0].length();int wordCount = words.length;for (int i = 0; i < wordLength; i++) {// 超出长度if (i + wordCount * wordLength > s.length()) {break;}// 初始化窗口Map<String, Integer> map = new HashMap<>();for (int j = 0; j < wordCount; j++) {String word = s.substring(i + j * wordLength, i + (j + 1) * wordLength);map.put(word, map.getOrDefault(word, 0) + 1);}// 筛掉原单词数组for (String word : words) {map.put(word, map.getOrDefault(word, 0) - 1);if (map.get(word) == 0) {map.remove(word);}}// 滑动窗口for (int j = 0; i + j + wordCount * wordLength <= s.length(); j+=wordLength) {if (j != 0) {String addWord = s.substring(i + j + wordLength * (wordCount - 1), i + j + wordLength * wordCount);map.put(addWord, map.getOrDefault(addWord, 0) + 1);if (map.get(addWord) == 0) {map.remove(addWord);}String delWord = s.substring(i + j - wordLength, i + j);map.put(delWord, map.getOrDefault(delWord, 0) - 1);if (map.get(delWord) == 0) {map.remove(delWord);}}if (map.size() == 0) {result.add(i + j);}}}return result;}
}

3.提交结果截图

在这里插入图片描述

4.图解

以如下测试用例举例说明:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]

首先,可以将字符串 s 按照单词长度进行划分,通过开头跳过字符长度的方式,可以分为以下三种划分方式。

在这里插入图片描述

以划分方式1举例,可以将所有单词总长度(单词数 * 单词长度)来作为一个窗口,从左往右滑动。

在这里插入图片描述

在这里插入图片描述

最终得到的 index=0index=9 就是我们的结果了。

整理完毕,完结撒花~ 🌻

http://www.15wanjia.com/news/25240.html

相关文章:

  • 简述网站开发的过程b2b电子商务网
  • php自己写框架做网站关键词seo排名优化如何
  • 网站诊断及优化方案高端网站建设定制
  • 靠谱高端网站设计首选品牌定位
  • 用asp做网站流程广州品牌seo推广
  • 衣服定制济南seo优化外包服务
  • 手机购物网站模板下载网络推广是什么工作内容
  • 做网站需要什么证件吗百度推广开户渠道
  • 发布的手机网站是乱码电商seo优化
  • 做网站 外文参考文献济宁网站建设
  • 深圳自定义网站开发seo引擎优化专员
  • 济南网站制作案例百度app推广方法
  • 专做滚针的网站百度seo发帖推广
  • go语做网站产品推广ppt
  • seo短视频网页入口引流在线观看网站seo推广的常见目的有
  • 河南国正建设集团公司网站seo优化技术是什么
  • 天津网站建设noajt营销推广渠道有哪些
  • 购物网站建设方案pptseo营销外包公司
  • 网站建设项目设计表怎样制作网站
  • 个人备案的网站关键词优化如何做
  • wordpress做旅游网站做网站推广
  • 网站建设建议互联网公司排名
  • 中山网站建设制作什么是营销型网站?
  • web做花店网站页面全国疫情最新情况公布
  • 做网站主图多少钱西安刚刚宣布
  • 帝国cms企业网站模板百度网站域名注册
  • 深圳b2c商城网站建设第三方营销平台有哪些
  • asp网站新闻置顶软文推广案例大全
  • 腾讯企点怎么群发哈尔滨优化推广公司
  • 天津网站策划百度收录方法