首页 > 代码库 > 30. Substring with Concatenation of All Words

30. Substring with Concatenation of All Words

一、题目

  1、描述

    技术分享

  2、题意

    字符串数组元素随意顺序全部拼接,求 s 中包含拼接后字符串的所有开始索引

 

二、解答

  1、思路:

    先将数组中元素进行全排序进行拼接,再遍历字符串 s 中所有包含的开始下标。

    // 30. Substring with Concatenation of All Words
    public static List<Integer> findSubstring(String s, String[] words) {
        
        if(s.equals(""))    return null;
        
        List<Integer> resultList = new ArrayList<Integer>();
        
        List<String> allStrlist = new ArrayList<String>();
        getAllStrList(allStrlist, words, 0 );
        
        int len = words[0].length() * words.length;
        for(int i = 0; i <= s.length() - len; i++) {
            for(String tmp: allStrlist) {
                if(s.subSequence(i,i+len).equals(tmp)) {
                    resultList.add(i);
                    break;
                }
            }
        }
        
        for(int i : resultList)
            System.out.println(i);
        
        return resultList;
    }

    //DFS:  全排列
    private static void getAllStrList(List<String> allStrlist, String[] words, int index) {
        
        if(index == words.length) {
            StringBuilder sb = new StringBuilder();
            for(String s: words)
                sb.append(s);
            allStrlist.add(sb.toString());
        }
        
        for (int i = index; i < words.length; i++) {
            swap(words, i, index);
            getAllStrList(allStrlist, words, index+1);
            swap(words, i, index);
        }
        
    }
    
    private static void swap(String[] words, int i, int index) {
        String temp = words[i];
        words[i] = words[index];
        words[index] = temp;
    }

  

  方法二:

    将字符串数组放入一个Map 中,遍历 字符串 s 来进行判断是否为满足的情况,用到了一个临时Map 来进行记录

public static List<Integer> findSubstring2(String s, String[] words) {
        List<Integer> list = new ArrayList<Integer>();
        Map<String, Integer> map = new HashMap<String, Integer>();
        Map<String, Integer> tmp = new HashMap<String, Integer>();
        
        int sLength = s.length();
        int wordsNum = words.length;
        int wordslength = words[0].length();
        int j;
        
        if(sLength < wordsNum || wordsNum == 0) 
            return list;
        for(int i = 0; i < wordsNum; i++) {
            if(map.containsKey(words[i]))
                map.put(words[i], map.get(words[i]) + 1);
            else
                map.put(words[i], 1);
        }
        
        for (int i = 0; i <= sLength-wordsNum*wordslength; i++) {
            tmp.clear();
            for(j = 0; j < wordsNum; j++) {
                String word = s.substring(i+j*wordslength, i + j*wordslength+wordslength);
                
                if(!map.containsKey(word)) 
                    break;
                
                // 可能有重复
                if(tmp.containsKey(word))
                    tmp.put(word, tmp.get(word)+1);
                else
                    tmp.put(word, 1);
                
                if(tmp.get(word) > map.get(word))
                    break;
            }
            
            if(j == wordsNum)
                list.add(i);
        }
        
        return list;
    }

 

30. Substring with Concatenation of All Words