Substring with Concatenation of All Words

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.



For example, given:

s: "barfoothefoobarman"

words: ["foo", "bar"]



You should return the indices: [0,9].

(order does not matter).

思路:leetcode 上有些题通过率低,并不见得是算法难,我认为非常大一部分原因是题目描写叙述不清晰。导致规则理解不透彻,所以编程的时候就会发生规则理解偏差的问题。

本题也是一样,刚一開始对规则理解偏差比較多,以为wors中字符出如今子串中出现一次就可以,无论反复还是不反复,可是后面提交只是时,看case全然理解错了规则,仅仅能又一次改写代码,非常麻烦。

怨言非常大,规则制定和说明也是非常重要的一点。

代码例如以下:

public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
if(words.length == 0 || s.length() == 0){
return list;
}
Map<String,Integer> map = new HashMap<String,Integer>();//保存个数以及值
for(int i = 0; i < words.length; i++){
if(map.get(words[i]) == null){
map.put(words[i],1);//将word保存
}else{
map.put(words[i],map.get(words[i])+1);//将word保存的数值+1
}
}
Map<String,Integer> mapValue = new HashMap<String,Integer>(map);//保存反复的个数,方便又一次赋值
int len = words.length;//去除反复之后的数组长度 int wordLen = words[0].length();
String temp = "";
int count = 0;//每一个单词出现一次,就记录一次
for(int i = 0; i <= s.length() - len*wordLen;i++){
count = 0;//初始化
for(int j = 0; j < len;j++){
temp = s.substring(i + j * wordLen,i + (j+1) * wordLen);//截取wordLen长的字符串
if(map.get(temp) != null && map.get(temp) != 0){//假设map还有多于0个
map.put(temp,map.get(temp)-1);//map中数值减去1
count++;//记录数+1
}else{
break;
}
}
if(count == len){//假设记录数与len相等。则说明符合要求
list.add(i);
}
//HashMap又一次初始化
for(String key:map.keySet()){//这样更高速
map.put(key,mapValue.get(key));
}
}
return list;
}
}

leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法的更多相关文章

  1. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  2. [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. Java [leetcode 30]Substring with Concatenation of All Words

    题目描述: You are given a string, s, and a list of words, words, that are all of the same length. Find a ...

  5. [leetcode]30. Substring with Concatenation of All Words由所有单词连成的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  6. LeetCode 30 Substring with Concatenation of All Words(确定包含所有子串的起始下标)

    题目链接: https://leetcode.com/problems/substring-with-concatenation-of-all-words/?tab=Description   在字符 ...

  7. [LeetCode] 30. Substring with Concatenation of All Words ☆☆☆

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  8. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  9. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

随机推荐

  1. cogs 服务点设置

    3. 服务点设置 ☆   输入文件:djsa.in   输出文件:djsa.out   简单对比时间限制:1 s   内存限制:128 MB 问题描述为了进一步普及九年义务教育,政府要在某乡镇建立一所 ...

  2. 17、Django实战第17天:机构详情展示

    1.进入xadmin添加测试数据(教师.课程) 2.把以下4个前端页面复制到templates中 先打开这几个页面分析,它们和之前的课程机构列表页是不一样的机构,且没有共同的部分,但是这4个页面却是类 ...

  3. ASP.NET Core 2.2 基础知识(十二) 发送 HTTP 请求

    可以注册 IHttpClientFactory 并将其用于配置和创建应用中的 HttpClient 实例. 这能带来以下好处: 提供一个中心位置,用于命名和配置逻辑 HttpClient 实例. 例如 ...

  4. jmeter bean shell断言加密的响应信息

    断言加密的响应信息 1.在http请求-->添加-->断言-->bean shell 断言 import com.changfu.EncryptAndDecryptInterface ...

  5. AxureRP7超强部件库打包下载

    摘要: 很多刚刚开始学习Axure的朋友都喜欢到网上搜罗各种部件库(组件库)widgets library ,但是网络中真正实用的并且适合你使用的少之又少,最好的办法就是自己制作适合自己工作内容的部件 ...

  6. Codeforces 138D World of Darkraft(Multi-Nim)

    [题目链接] http://codeforces.com/problemset/problem/138/D [题目大意] H*W的棋盘中每个点都是L.R.X三者之一,两人轮流选一个点, 若为L则向左下 ...

  7. 【递归】先修课 计算概论(A) / 函数递归练习(3)2:分解因数

    #include<cstdio> using namespace std; bool is_prime(int x) { ;i*i<=x;i++) ) return false; r ...

  8. 显示ACSII码字符表 Exercise05_15

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:显示ACSII码字符表 * */ public class Exercise05_15 { public static voi ...

  9. P2P通信标准协议(二)之TURN

    上一篇P2P通信标准协议(一)介绍了在NAT上进行端口绑定的通用规则,应用程序可以根据这个协议来设计网络以外的通信. 但是,STUN/RFC5389协议里能处理的也只有市面上大多数的Cone NAT( ...

  10. Devexpress汉化修改 已经汉化过后生成的*.resources.dll文件 z

    Devexpress 是很好的第三方控件.但是需要汉化,在从网上下载的zh-CN文件夹里都是*.resources.dll文件,由于汉化的工作量很大,难免有些地方汉化 错误或者不合适(如下图).花了些 ...