【030-Substring with Concatenation of All Words(串联全部单词的子串)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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).

题目大意

  给定一个字符串s和一个字符串数组words,wrods中的字符串长度都相等。找出s中全部的子串恰好包括words中全部字符各一次,返回子串的起始位置。

解题思路

  把words转化为一个HashMap

代码实现

算法实现类

import java.util.*;

public class Solution {

    public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
if (words.length == 0) return list;
int wLen = words[0].length();
int len = s.length();
if (len < wLen * words.length) return list;
Map<String, Integer> mapW = new HashMap<String, Integer>();
for (String word : words)
mapW.put(word, mapW.containsKey(word) ? mapW.get(word) + 1 : 1);
for (int start = 0; start < wLen; start++) {
int pos = start;
int tStart = -1;
Map<String, Integer> mapT = new HashMap<String, Integer>(mapW);
while (pos + wLen <= len) {
String cand = s.substring(pos, pos + wLen);
if (!mapW.containsKey(cand)) {
if (tStart != -1) mapT = new HashMap<String, Integer>(mapW);
tStart = -1;
} else if (mapT.containsKey(cand)) {
tStart = tStart == -1 ? pos : tStart;
if (mapT.get(cand) == 1) mapT.remove(cand);
else mapT.put(cand, mapT.get(cand) - 1);
if (mapT.isEmpty()) list.add(tStart);
} else {
while (tStart < pos) {
String rCand = s.substring(tStart, tStart + wLen);
if (cand.equals(rCand)) {
tStart += wLen;
if (mapT.isEmpty()) list.add(tStart);
break;
}
tStart += wLen;
mapT.put(rCand, mapT.containsKey(rCand) ? mapT.get(rCand) + 1 : 1);
}
}
pos += wLen;
}
}
return list;
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47064933

【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】的更多相关文章

  1. [LeetCode] 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 ...

  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-面试算法经典-Java实现】【058-Length of Last Word (最后一个单词的长度)】

    [058-Length of Last Word (最后一个单词的长度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s consis ...

  4. 030 Substring with Concatenation of All Words 与所有单词相关联的字串

    给定一个字符串 s 和一些长度相同的单词 words,找出 s 与 words 中所有单词(words 每个单词只出现一次)串联一起(words中组成串联串的单词的顺序随意)的字符串匹配的所有起始索引 ...

  5. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  6. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  7. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  8. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

  9. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

随机推荐

  1. PhpStorm 快捷键大全 PhpStorm 常用快捷键和配置+如何关闭快捷键ctrl+alt+方向键旋转屏幕

    如果映射的是eclipse的快捷键,又同时安装了英特尔的GPU软件,那么会有这个快捷键冲突 就是快速复制快捷键:ctrl+alt+方向键  会调用英特尔旋转屏幕,禁用掉即可 PhPStorm 是 Je ...

  2. Visual Studio跨平台开发(5):Xamarin Android多页面应用开发

    前言 大部份的Android 都具有实体或虚拟的Back键. 因此在处理多页面应用程序时, 与先前所介绍的iOS Navigation controller 比较起来会简单许多. 1. 开启Visua ...

  3. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  4. luogu P1145 约瑟夫

    题目描述 n个人站成一圈,从某个人开始数数,每次数到m的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人,k个好人站在一起,k个坏人站在一起.从第一个好人开始数数.你要确定一个最小 ...

  5. La 4976 Defense lines

    蓝书紫书上都有的一道题...这里就懒得说题解了. 但是我竟然WA了6次!为什么呢??? 一开始没看见连续子序列..... 后来插入的时候忘判断了是不是比前驱大.... 所以我们只需要维护一个权值递增( ...

  6. 面向对象-QuickHit项目

    package com.ketang.game; /** * 游戏级别类 * @author * */ public class Level { private int levelNo; //各级别编 ...

  7. 四. Java继承和多态4. 多态和动态绑定

    在Java中,父类的变量可以引用父类的实例,也可以引用子类的实例. 请读者先看一段代码: public class Demo { public static void main(String[] ar ...

  8. 三. Java类与对象8.再谈Java包

    在Java中,为了组织代码的方便,可以将功能相似的类放到一个文件夹内,这个文件夹,就叫做包. 包不但可以包含类,还可以包含接口和其他的包. 目录以"\"来表示层级关系,例如 E:\ ...

  9. Laravel 使用firstOrCreate 报错MassAssignmentException

    今天尝试使用firstOrCreate去优化一段查找不到即创建的代码,结果发现会报MassAssignmentException错误,提示我参数错误,去网上找了好久没有找到结果,最后庆幸自己解决了,把 ...

  10. nat的翻译类型(1)--静态nat

    目的:在1.1 1.2 1.3 三台内网的服务器访问外网的服务器(202.1.1.2)时,将内网ip转换为外网ip. 1.设置内网三台服务器的Ip ,网关,以及外网服务器的ip网关 分别为:192.1 ...