Advertisement
1988coder

30. Substring with Concatenation of All Words

Oct 25th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. /**
  2. Refer: https://leetcode.com/problems/substring-with-concatenation-of-all-words/discuss/13656/An-O(N)-solution-with-detailed-explanation
  3.  
  4. Time Complexity = O(length of the string * length of one word)
  5. Spcae Complexity = O(words.length)
  6. */
  7. class Solution {
  8.     public List<Integer> findSubstring(String s, String[] words) {
  9.         List<Integer> result = new ArrayList<>();
  10.         if (s == null || words == null || words.length == 0 || s.length() < words.length * words[0].length()) {
  11.             return result;
  12.         }
  13.        
  14.         HashMap<String, Integer> countMap = new HashMap<>();
  15.         for (String word : words) {
  16.             countMap.put(word, countMap.getOrDefault(word, 0) + 1);
  17.         }
  18.         int n = s.length();
  19.         int numOfWords = words.length;
  20.         int sizeOfWord = words[0].length();
  21.        
  22.         for (int i = 0; i < sizeOfWord; i++) {
  23.             int count = 0;
  24.             int low = i;
  25.             HashMap<String, Integer> seenMap = new HashMap<>();
  26.             for (int j = i; j <= n-sizeOfWord; j += sizeOfWord) {
  27.                 String str = s.substring(j, j+sizeOfWord);
  28.                 if (!countMap.containsKey(str)) {
  29.                     seenMap = new HashMap<>();
  30.                     count = 0;
  31.                     low = j+sizeOfWord;
  32.                 } else {
  33.                     seenMap.put(str, seenMap.getOrDefault(str, 0) + 1);
  34.                     count++;
  35.                     while (seenMap.get(str) > countMap.get(str)) {
  36.                         String toBeRemoved = s.substring(low, low+sizeOfWord);
  37.                         seenMap.put(toBeRemoved, seenMap.get(toBeRemoved) - 1);
  38.                         count--;
  39.                         low += sizeOfWord;
  40.                     }
  41.                     if (count == numOfWords) {
  42.                         result.add(low);
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.         return result;
  48.     }
  49. }
  50.  
  51. /**
  52. Refer : https://leetcode.com/problems/substring-with-concatenation-of-all-words/discuss/13658/Easy-Two-Map-Solution-(C++Java)
  53.  
  54. Time Complexity: O(lengthOfString * numOfWords)
  55. Space Complexity: O(numOfWords)
  56. */
  57. class Solution {
  58.     public List<Integer> findSubstring(String s, String[] words) {
  59.         List<Integer> result = new ArrayList<>();
  60.         if (s == null || words == null || words.length == 0 || s.length() < words.length * words[0].length()) {
  61.             return result;
  62.         }
  63.        
  64.         HashMap<String, Integer> countMap = new HashMap<>();
  65.         for (String word : words) {
  66.             countMap.put(word, countMap.getOrDefault(word, 0) + 1);
  67.         }
  68.         int n = s.length();
  69.         int numOfWords = words.length;
  70.         int sizeOfWord = words[0].length();
  71.        
  72.         for (int i = 0; i < (n - numOfWords * sizeOfWord + 1); i++) {
  73.             HashMap<String, Integer> seenMap = new HashMap<>();
  74.             int count = 0;
  75.             for (int j = i; j < i + numOfWords * sizeOfWord; j += sizeOfWord) {
  76.                 String word = s.substring(j, j+sizeOfWord);
  77.                 if (!countMap.containsKey(word)) {
  78.                     break;
  79.                 }
  80.                 seenMap.put(word, seenMap.getOrDefault(word, 0) + 1);
  81.                 if (seenMap.get(word) > countMap.get(word)) {
  82.                     break;
  83.                 }
  84.                 count++;
  85.             }
  86.             if (count == numOfWords) {
  87.                 result.add(i);
  88.             }
  89.         }
  90.         return result;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement