Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255905
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-04-16 11:15:58

//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.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class SubstringwithConcatenationofAllWords {


public static void main(String[] args) {
// TODO 自动生成的方法存根
ArrayList list=new ArrayList<>();
list.add("");
System.out.print(findSubstring("sffsa",list ));


}
public static List findSubstring(String S, List L) {
        ArrayList list=new ArrayList<>();
if(L.size()<1)
return list;
int len=L.get(0).length();
Map map=new HashMap();
for(String s:L){
if(!map.containsKey(s)){
map.put(s, 1);
}
else{
map.put(s,map.get(s)+1);
}
}
boolean flag;
for(int i=0;i<=S.length()-L.size()*len;i++){
int start=i;
Map temp=new HashMap(map);
//针对空的
flag=false;
//确保量是充足的
while(start-i flag=true;
String string=S.substring(start,start+len);
if(temp.containsKey(string)&&temp.get(string)>0)
temp.put(string,temp.get(string)-1);
else{
flag=false;
break;

}
start+=len;


}
if(flag){
list.add(i);
}
}
        return list;
    }


}

阅读(1040) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~