leetcode — substring-with-concatenation-of-all-words
import java.util.*;
/**
* Source : https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/
*
* Created by lverpeng on 2017/7/13.
*
* You are given a string, S, and a list of words, L, that are all of the same length.
* Find all starting indices of substring(s) in S that is a concatenation of each word
* in L exactly once and without any intervening characters.
*
* For example, given:
* S: "barfoothefoobarman"
* L: ["foo", "bar"]
*
* You should return the indices: [0,9].
* (order does not matter).
*
*
* 找出L中单词连接成的子串在字符串S中出现所有位置
*
*/
public class SubstringWithConcatenationOfAllWords {
/**
*
* 将strArr中的所有单词放在hash表中,单词为key,相同单词出现的次数为value
*
* strArr中所有单词长度一致,S重要出现所有单词的连接成为的字符串,那么S的长度一定要大于strArr中所有单词的总长度,
* 也就是起始字符在0-(S.length - strArr.length * strArr[0].length)之间,
* 以上面的位置为起始位置,依次判断接下来的strArr.length个单词是否正好是上面hash表中的单词,并判断相同单词出现次数,
* 如果没有出现则退出循环,如果出现的次数大于hash表中单词的次数也break
* 一轮循环完成之后判断循环的次数是否正好是strArr.length,如果是,说明S包含strArr连接的字符串,记录此时的起始位置到结果中
*
* @param S
* @param strArr
* @return
*/
public int[] findSubstring (String S, String[] strArr) {
if (S.length() < 1 | strArr.length < 1) {
return new int[]{};
}
Map<String, Integer> wordMap = new HashMap<String, Integer>(); // 存放strArr单词的哈希表
for (String str : strArr) {
if (wordMap.keySet().contains(str)) {
wordMap.put(str, wordMap.get(str) + 1);
} else {
wordMap.put(str, 1);
}
}
int loopCount = 0;
int arrLen = strArr.length;
int wordLen = strArr[0].length();
int arrStrLen = arrLen * wordLen;
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i < S.length() - arrStrLen; i++) {
int j = 0;
Map<String, Integer> subStrMap = new HashMap<String, Integer>();
for (j = 0; j < arrLen; j++) {
loopCount ++;
String subStr = S.substring(i + j * wordLen, i + j * wordLen + wordLen);
if (!wordMap.keySet().contains(subStr)) {
break;
} else {
if (subStrMap.keySet().contains(subStr)) {
subStrMap.put(subStr, subStrMap.get(subStr) + 1);
} else {
subStrMap.put(subStr, 1);
}
}
if (subStrMap.get(subStr) > wordMap.get(subStr)) {
break;
}
}
if (j == arrLen) {
result.add(i);
}
}
System.out.println("loopCount------->" + loopCount);
int[] res = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
res[i] = result.get(i);
}
return res;
}
/**
* 上面是以步长为1进行循环,下面以步长为word长度进行循环
*
* @param S
* @param strArr
* @return
*/
public int[] findSubstring1 (String S, String[] strArr) {
if (S.length() < 1 || strArr.length < 1) {
return new int[]{};
}
Map<String, Integer> wordMap = new HashMap<String, Integer>();
for (int i = 0; i < strArr.length; i++) {
if (wordMap.keySet().contains(strArr[i])) {
wordMap.put(strArr[i], wordMap.get(strArr[i]));
} else {
wordMap.put(strArr[i], 1);
}
}
List<Integer> result = new ArrayList<Integer>();
int wordLen = strArr[0].length();
Map<String, Integer> subStrMap = new HashMap<String, Integer>();
int loopCount = 0;
for (int i = 0; i < wordLen; i++) {
int count = 0;
int left = i; // 记录待匹配子串起始位置
for (int j = i; j < S.length() - wordLen; j += wordLen) {
loopCount ++;
String subStr = S.substring(j, j + wordLen);
if (wordMap.keySet().contains(subStr)) {
if (subStrMap.keySet().contains(subStr)) {
subStrMap.put(subStr, subStrMap.get(subStr) + 1);
} else {
subStrMap.put(subStr, 1);
}
count ++;
if (subStrMap.get(subStr) <= wordMap.get(subStr)) {
count ++;
} else {
// 说明当前开始位置不匹配
while (subStrMap.get(subStr) > wordMap.get(subStr)) {
//
String startWord = S.substring(left, left + wordLen);
subStrMap.put(startWord, subStrMap.get(startWord) - 1);
left += wordLen;
count --;
}
}
if (count == strArr.length) {
// 找到了
result.add(left);
// 向后移动一个单词
count --;
String startWord = S.substring(left, left + wordLen);
subStrMap.put(startWord, subStrMap.get(startWord) - 1);
left += wordLen;
}
} else {
// 清空变量,重新开始查找
left = j + wordLen;
subStrMap.clear();
count = 0;
}
}
}
System.out.println("loopCount------->" + loopCount);
int[] res = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
res[i] = result.get(i);
}
return res;
}
public static void main(String[] args) {
SubstringWithConcatenationOfAllWords substringWithConcatenationOfAllWords = new SubstringWithConcatenationOfAllWords();
String S = "barfoothefoobarman";
String[] strArr = new String[]{"foo", "bar"};
System.out.println(Arrays.toString(substringWithConcatenationOfAllWords.findSubstring(S, strArr)));
System.out.println(Arrays.toString(substringWithConcatenationOfAllWords.findSubstring1(S, strArr)));
}
}
leetcode — substring-with-concatenation-of-all-words的更多相关文章
- LeetCode: Substring with Concatenation of All Words 解题报告
Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...
- [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 ...
- LeetCode:Substring with Concatenation of All Words (summarize)
题目链接 You are given a string, S, and a list of words, L, that are all of the same length. Find all st ...
- [leetcode]Substring with Concatenation of All Words @ Python
原题地址:https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/ 题意: You are given a ...
- Leetcode Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- [LeetCode] Substring with Concatenation of All Words(good)
You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...
- Leetcode:Substring with Concatenation of All Words分析和实现
题目大意是传入一个字符串s和一个字符串数组words,其中words中的所有字符串均等长.要在s中找所有的索引index,使得以s[index]为起始字符的长为words中字符串总长的s的子串是由wo ...
- LeetCode()Substring with Concatenation of All Words 为什么我的超时呢?找不到原因了!!!
超时代码 class Solution { public: vector<int> findSubstring(string s, vector<string>& wo ...
- LeetCode HashTable 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 ...
- leetcode面试准备: Substring with Concatenation of All Words
leetcode面试准备: Substring with Concatenation of All Words 1 题目 You are given a string, s, and a list o ...
随机推荐
- VS2015 提示 无法启动 IIS Express Web 服务器
好久没有写东西了,不是没的写,是没时间了,今天快下班了,正好遇到这个一个问题,我就记录下来,以防忘记. 我定义了一个项目,Demo代码也写好了,然后,我们就把写好的项目代码加入到了源代码管理工具里.然 ...
- 在IIS7里配置 ISAPI,运行dll程序,总提示下载dll
在IIS7里配置 ISAPI,运行dll程序,总提示下载dll,只需要把对应站点应用程序池里面的高级设置里的启用32位应用程序,设为“true"即可.
- 九校联考_24OI——餐馆restaurant
凉心模拟D1T1--最简单的一道题 TAT 餐馆(restaurant) 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 题目描述 共有n 种食材,一份食材i 需要花ti 小 ...
- python基础之Day15
一.函数递归 什么是函数递归: 函数递归调用是一种特殊的嵌套调用,在调用一个函数的过程中,又直接或间接地调用了该函数本身. 其中,函数的递归有明确的结束条件,不能无限制的调用,否则会撑破内存,在Pyt ...
- FTP服务与配置
FTP简介 网络文件共享服务主流的主要有三种,分别是ftp.nfs.samba. FTP是File Transfer Protocol(文件传输协议)的简称,用于internet上的控制文件的双向传输 ...
- Catalan数与出栈顺序个数,Java编程模拟
问题描述: 队列中有从1到7(由小到大排列)的7个整数,问经过一个整数栈后,出栈的所有排列数有多少?如果整数栈的容量是4(栈最多能容纳4个整数),那么出栈的排列数又是多少? 分析:对于每一个数字i, ...
- java基础-三元运算符
1.三元运算符的格式 /* 三元运算符 (条件表达式)?表达式1:表达式2; 如果条件为true,整个表达式结果是表达式1: 如果条件为false,整个表达式结果是表达式2: 注意:三元运算符不能单独 ...
- Docker集群管理工具 - Kubernetes 部署记录 (运维小结)
一. Kubernetes 介绍 Kubernetes是一个全新的基于容器技术的分布式架构领先方案, 它是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernete ...
- AddTransient,AddScope和AddSingleton 有什么不同?
我们先来创建几个接口using System; namespace DependencyInjectionSample.Interfaces{ public interface IOperation ...
- 如何减少SQL Server中的PREEMPTIVE_OS_WRITEFILEGATHER等待类型
在数据库大小分配期间,我正在等待类型PREEMPTIVE_OS_WRITEFILEGATHER.昨天,我将数据库大小配置为供应商建议的值.我们需要将数据库大小设置为700GB,保留150 GB的日志文 ...