LeetCode--Substring with Concatenation of All Words(java)

来源:互联网 发布:中国网民数据 编辑:程序博客网 时间:2024/06/15 04:44
<p style="font-size: 14px; box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 30px;">You are given a string, <span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box; font-weight: 700;">s</span>, and a list of words, <span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box; font-weight: 700;">words</span>, that are all of the same length. Find all starting indices of substring(s) in <span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box; font-weight: 700;">s</span> that is a concatenation of each word in <span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box; font-weight: 700;">words</span>exactly once and without any intervening characters.</p><p style="font-size: 14px; box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 30px;">For example, given:<br style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box;" /><span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box; font-weight: 700;">s</span>: <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 2px 4px; color: rgb(199, 37, 78); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(249, 242, 244);">"barfoothefoobarman"</code><br style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box;" /><span style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box; font-weight: 700;">words</span>: <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 2px 4px; color: rgb(199, 37, 78); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(249, 242, 244);">["foo", "bar"]</code></p><p style="font-size: 14px; box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 30px;">You should return the indices: <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 2px 4px; color: rgb(199, 37, 78); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(249, 242, 244);">[0,9]</code>.<br style="font-family: 'Microsoft YaHei', SimSun, Verdana, Arial, Helvetica, sans-serif; line-height: 1.5; box-sizing: border-box;" />(order does not matter).</p>
import java.util.*;public class Solution {    public List<Integer> findSubstring(String s, String[] words) {        int length = words[0].length();        int tmpb=-1;        HashMap<String,Integer> map = new HashMap<String,Integer>();        HashMap<String,Integer> map2 = new HashMap<String,Integer>();        List<String> list = new LinkedList<String>();        List<Integer> list2 = new ArrayList<Integer>();        //List<Integer> list2 = new List<Integer>();        for (int i=0;i<words.length;i++){//System.out.println(list);            if (!map2.containsKey(words[i]))map2.put(words[i],1);else map2.put(words[i],map2.get(words[i])+1);map.put(words[i],0);//System.out.println(map2.get(words[i]));}for (int p = 0; p < length; p++) {//要以length来避免找到同余的位置..        for (int i = p;i<=s.length()-length;)        {if ((words.length-list.size())*length>s.length()-i)break;            String tmp = s.substring(i,i+length);            if (map.containsKey(tmp)){                if (map.get(tmp)==0)                 {                        map.put(tmp,1);                     if (tmpb<0)                        tmpb=i;                     i+=length;                     list.add(tmp);                    if (list.size()==words.length)                        {list2.add(i-words.length*length);                            tmpb=-1;                        }                    //if (i+length*words.length>s.length())break;//                 }                else if (map.get(tmp)==map2.get(tmp)&&list.size()!=0)                {//System.out.println(2);                    while(!list.get(0).equals(tmp))                    {                        map.put(list.get(0),map.get(list.get(0))-1);                        list.remove(0);                    }                    list.remove(0);                    list.add(tmp);i+=length;                    if (list.size()==words.length)                        {                            list2.add(i-words.length*length);                            tmpb=-1;                        }                }else if (map.get(tmp)<map2.get(tmp)){//System.out.println(3);i+=length;map.put(tmp,map.get(tmp)+1);list.add(tmp);                    if (list.size()==words.length)                        {                            list2.add(i-words.length*length);                        }}}            else            {   //System.out.println(4);                while(list.size()>0)                {                    map.put(list.get(0),map.get(list.get(0))-1);                    list.remove(0);                }                //i++;i+=length;if (i+length*words.length>s.length())break;            }//System.out.println(list.size());        }                while(list.size()>0)                {                    map.put(list.get(0),map.get(list.get(0))-1);                    list.remove(0);                }}        return list2;    }}

0 0
原创粉丝点击