【LeetCode-面試演算法經典-Java實現】【030-Substring with Concatenation of All Words(串聯全部單詞的子串)】

來源:互聯網
上載者:User

標籤:檔案夾   cti   arm   .text   說明   length   返回   gcs   title   

【030-Substring with Concatenation of All Words(串聯全部單詞的子串)】【LeetCode-面試演算法經典-Java實現】【全部題目檔案夾索引】原題

  You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.
  For example, given:
  s: "barfoothefoobarman"
  words: ["foo", "bar"]
  You should return the indices: [0,9].
  (order does not matter).

題目大意

  給定一個字串s和一個字串數組words,wrods中的字串長度都相等。找出s中全部的子串恰好包括words中全部字元各一次,返回子串的起始位置。


解題思路

  把words轉化為一個HashMap

代碼實現

演算法實作類別

import java.util.*;public class Solution {    public List<Integer> findSubstring(String s, String[] words) {        List<Integer> list = new ArrayList<Integer>();        if (words.length == 0) return list;        int wLen = words[0].length();        int len = s.length();        if (len < wLen * words.length) return list;        Map<String, Integer> mapW = new HashMap<String, Integer>();        for (String word : words)            mapW.put(word, mapW.containsKey(word) ? mapW.get(word) + 1 : 1);        for (int start = 0; start < wLen; start++) {            int pos = start;            int tStart = -1;            Map<String, Integer> mapT = new HashMap<String, Integer>(mapW);            while (pos + wLen <= len) {                String cand = s.substring(pos, pos + wLen);                if (!mapW.containsKey(cand)) {                    if (tStart != -1) mapT = new HashMap<String, Integer>(mapW);                    tStart = -1;                } else if (mapT.containsKey(cand)) {                    tStart = tStart == -1 ?

pos : tStart; if (mapT.get(cand) == 1) mapT.remove(cand); else mapT.put(cand, mapT.get(cand) - 1); if (mapT.isEmpty()) list.add(tStart); } else { while (tStart < pos) { String rCand = s.substring(tStart, tStart + wLen); if (cand.equals(rCand)) { tStart += wLen; if (mapT.isEmpty()) list.add(tStart); break; } tStart += wLen; mapT.put(rCand, mapT.containsKey(rCand) ? mapT.get(rCand) + 1 : 1); } } pos += wLen; } } return list; }}

評測結果

  點擊圖片,滑鼠不釋放,拖動一段位置,釋放後在新的表單中查看完整圖片。

特別說明歡迎轉載。轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/47064933】

【LeetCode-面試演算法經典-Java實現】【030-Substring with Concatenation of All Words(串聯全部單詞的子串)】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.