问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次。

算法分析:这道题和strStr很类似。只不过strStr是子串,而这个题是字符串数组里的元素组成的子串,字符串数组里的元素是无序的,但是必须全部包含。所有考虑使用map集合。关键点在于几个临界值,字符串元素在s中重复了怎么做,找到一个符合的子串后怎么做,有字符串元素不匹配怎做。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; public class SubstringwithConcatenationofAllWords { public List<Integer> findSubstring(String s, String[] words) { ArrayList<Integer> result = new ArrayList<Integer>();
if(s==null||s.length()==0||words==null||words.length==0){
return result;
} //frequency of words
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(String w: words){
if(map.containsKey(w)){
map.put(w, map.get(w)+1);
}else{
map.put(w, 1);
}
} int len = words[0].length(); for(int j=0; j<len; j++)
{
HashMap<String, Integer> currentMap = new HashMap<String, Integer>();
int start = j;//起始下标,因为len为窗口大小,所以起始下标只需在0-len之间循环,就像取模一样,超过了,就重复了。
int count = 0;//记录满足的字符串数量 for(int i=j; i<=s.length()-len; i=i+len)
{
String sub = s.substring(i, i+len);
if(map.containsKey(sub))
{
if(currentMap.containsKey(sub))
{
currentMap.put(sub, currentMap.get(sub)+1);
}
else
{
currentMap.put(sub, 1);
} count++;
//只要发现子字符串中有重复的字符数组中的元素,那么就从起始位置循环删除字符串.
//也就是说重复了,就逆向操作,直至只包含一个重复的那个字符串。
while(currentMap.get(sub)>map.get(sub))
{
//四个基本逆向操作。重复了或匹配成功了都要进行窗口滑动。
String left = s.substring(start, start+len);
currentMap.put(left, currentMap.get(left)-1);
count--;
start = start + len;
} if(count==words.length)//字符串包含所有字符数组中的元素,
{
result.add(start); //将起始位置加入result
//四个基本逆向操作。窗口滑动。窗口值为len
String left = s.substring(start, start+len);
currentMap.put(left, currentMap.get(left)-1);
count--;
start = start + len;
}
}
else
{
currentMap.clear();
start = i+len;
count = 0;
}
}
} return result;
}
}

StrStr算法如下:

public int strStr(String haystack, String needle) {

        if(haystack == null || needle == null)
{
return -1;
} for (int i = 0; i <= (haystack.length()-needle.length()); i++)
{
int m = i;
for (int j = 0; j < needle.length(); j++)
{
if (needle.charAt(j) == haystack.charAt(m))
{
m++;
if((m-i) == needle.length())
{
return i;
}
}
else
{
break;
}
}
}
return -1;
}

Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置的更多相关文章

  1. java中怎么判断一个字符串中包含某个字符或字符串

    public static void main(String[] args) { String str="ABC_001"; ){ System.out.println(" ...

  2. String substring(int start,int end)截取当前字符串中指定范围内的字符串

    package seday01;/** * String substring(int start,int end) * 截取当前字符串中指定范围内的字符串. * java api有一个特点:通常用两个 ...

  3. C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,

    //将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换. #include <stdio.h> #include <string.h> void fun ...

  4. JavaScript确定一个字符串是否包含在另一个字符串中的四种方法

    一.indexOf() 1.定义 indexOf()方法返回String对象第一次出现指定字符串的索引,若未找到指定值,返回-1.(数组同一个概念) 2.语法 str.indexOf(searchVa ...

  5. Oracle字符串中包含数字、特殊符号的排序

    问题描述: 某小区,需要按照小区.楼栋.单元号.房间号进行排序,但是按照地址描述排序时,因为字符串中包含数字,所以造成了如下的结果, 1号楼之后应该是2号楼,但是查询结果却是10号楼 . 尝试解决 使 ...

  6. 【转载】C#通过IndexOf方法判断某个字符串是否包含在另一个字符串中

    C#开发过程中针对字符串String类型的操作是常见操作,有时候需要判断某个字符串是否包含在另一个字符串,此时可以使用IndexOf方法以及Contain方法来实现此功能,Contain方法返回Tru ...

  7. C语言:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动。-使字符串的前导*号不得多于n个,若多余n个,则删除多余的*号,

    //fun函数:从p所指字符串中找出ASCII码最大的字符,将其放在第一个位置上,并将该字符前的原字符向后顺序移动. #include <stdio.h> void fun( char * ...

  8. C# String.Format格式化json字符串中包含"{" "}"报错问题

    json.Append(String.Format("{\"total\":{0},\"row\":{1}}", lineCount, st ...

  9. PHP & Javascript 如何对字符串中包含html标签进行编码 整理

    为什么要对字符串编码? 某些字符串中包含html标签,不编码,页面输出就乱了. PHP下怎么对字符串编码? htmlentities vs htmlspecialchars htmlentities ...

随机推荐

  1. javajava持有对象(容器类)

    数组是固定的长度去保存对象,还有一些集合类如基本的List.Set.Quene.Map可以存储不固定长度的对象.

  2. mysql中排序

    排序(默认:asc升序; desc降序 如:根据成绩从高到低排序 select * from stu_info order by mark desc; 根据成绩从低到高排序 select * from ...

  3. Exchange Version and UpdateRollups

    Exchange Server 2010 Product name Build number Date KB Microsoft Exchange Server 2010 RTM 14.0.639.2 ...

  4. wire_format.cc:1091] String field 'accountid' contains invalid UTF-8 data when serializing a protocol buffer. Use the 'bytes' type if you intend to send raw bytes.

    原因: 在protobuf 的string字段中存在中文,序列化的时候会出现截断数据,string这个类型带有检查功能 解决方法: 把protobuf中存在中文的string字段类型 改为bytes ...

  5. sqlalchemy(二)高级用法 2

    转自:https://www.cnblogs.com/coder2012/p/4746941.html 外键以及relationship 首先创建数据库,在这里一个user对应多个address,因此 ...

  6. logging/re - 总结

    logging 模块 很多程序都有记录日志的需求 logging的日志可以分为 debug(), info(), warning(), error() and critical()5个级别 1.输出到 ...

  7. DHTML 简介

    DHTML, 动态的 html, 不是一门语言, 是多项技术综合体的简称.其中包括了 html, CSS, DOM, javascript. HTML : 负责提供标签, 对数据进行封装,目的是便于对 ...

  8. 解决putty自动断开的问题

    解决putty自动断开的问题 putty窗口上右键>change settings.打开后如下,修改seconds of keepalives,让putty每隔若干秒发送心跳包

  9. 6.Insert Documents-官方文档摘录

    总结 1.插入单文档 db.inventory.insertOne( { item: "canvas", qty: , tags: , w: 35.5, uom: "cm ...

  10. 在启动MYSQL时出现问题:“ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)”

    1.问题描述 在启动MYSQL时出现问题:"ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)& ...