30. Strings associated with all words, Java implementations

Source: Internet
Author: User

Title Description:

Given a string s and some words of the same length words. Find the starting position of the substring in s that can exactly concatenate all the words in the words.

Note that the substring should exactly match the words in the words, no other characters in the middle, but no need to consider the sequence of words in the words.

示例 1:输入: s = "barfoothefoobarman", words = ["foo","bar"]输出:   [0,9]解释: 从索引 0 和 9 开始的子串分别是 "barfoor" 和 "foobar" 。输出的顺序不重要, [9,0] 也是有效答案。
示例 2:输入: s = "wordgoodstudentgoodword", words = ["word","student"]输出: []
Sliding Window Method:
    Reference blog://http://www.cnblogs.com/migoo/p/9454684.html//using a sliding window method public list<integer> findsubstring (String s, string[] words)        {list<integer> result = new arraylist<integer> (); If s, or words is empty, then also returns an empty list if (s.length () = = 0 | | s = = NULL | | words.length = = 0 | | words = = NULL) {R        Eturn result;        } int size = Words[0].length (), length = Words.length;        Insert the string in the string array into HashMap hashmap<string, integer> map = generate (words); Different beginnings of the window, with a size different starting point for (int i = 0; i < size; i++) {hashmap<string, integer> window= new Ha  Shmap<> ();            A sliding window int left,right;            left = right = i; while (right <= s.length ()-size && left <= s.length ()-length * size) {String Word = s.sub                String (right, right + size);                INCR (window, word);                   if (!map.containskey (word)) { Window.clear ();                    Right + = size;                    left = right;                Continue } while (Window.get (word) > Map.get (Word)) {String w = s.substring (left, left + size)                    ;                    DECR (window, w);                Left + = size;                } right + = size;                if (Right-left = = Size * Length) {Result.add (left);    }}} return result; } private hashmap<string, integer> generate (string[] strs) {hashmap<string, integer> map = new HASHM        Ap<> ();        for (String str:strs) {incr (map, str);    } return map;     } private void Incr (hashmap<string, integer> map, String str) {map.put (str, map.getordefault (str,0) + 1);        } private void Decr (hashmap<string, integer> map, String str) {Integer num = map.get (str); if (Num <= 1) {map.remove (str);        }else {map.put (str, num-1); }    }
Violence Law:
    Reference blog//https://www.nowcoder.com/discuss/87526?type=0&order=0&pos=11&page=0//Violence Law public List <Integer> findsubstring (String s, string[] words) {list<integer> result = new arraylist<integer>        ();        if (s.length () = = 0 | | s = = NULL | | words.length = = 0 | | words = = NULL) {return result;        } int size = Words[0].length ();        int length = Words.length; When intercepting a string, take the left and not the right, so the maximum value of I in the For loop here can take an equal sign for (int i = 0; I <= s.length ()-size * length; i++) {hashmap& Lt            String, integer> map = new hashmap<> ();            for (String word:words) {map.put (Word, map.getordefault (Word, 0) + 1);            } if (check (s,i,map,size)) {result.add (i);    }} return result;            } Private Boolean check (String s, int i, hashmap<string, integer> map, int size) {if (map.size () = = 0) {        return true;      }  if (i > s.length () | | i + size > s.length ()) {return false;        } String Word = s.substring (i, i + size);        if (!map.containskey (word)) {return false;            }else {Integer num = map.get (word);            if (num <= 1) {map.remove (word);            }else {map.put (Word, num-1);        } return check (s, i + size, map, size); }    }

30. Strings, Java implementations associated with all words

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.