Skip to content

Commit

Permalink
Merge pull request #100 from ritikpatel17/patch-4
Browse files Browse the repository at this point in the history
 Substring with Concatenation of All Words
  • Loading branch information
avastino7 authored Nov 1, 2022
2 parents 6b35abf + 4b1ce8a commit fe33211
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class Solution {
private HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
private int wordLength;
private int substringSize;
private int k;

private boolean check(int i, String s) {
// Copy the original dictionary to use for this index
HashMap<String, Integer> remaining = new HashMap<>(wordCount);
int wordsUsed = 0;

// Each iteration will check for a match in words
for (int j = i; j < i + substringSize; j += wordLength) {
String sub = s.substring(j, j + wordLength);
if (remaining.getOrDefault(sub, 0) != 0) {
remaining.put(sub, remaining.get(sub) - 1);
wordsUsed++;
} else {
break;
}
}

return wordsUsed == k;
}

public List<Integer> findSubstring(String s, String[] words) {
int n = s.length();
k = words.length;
wordLength = words[0].length();
substringSize = wordLength * k;

for (String word : words) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}

List<Integer> answer = new ArrayList<>();
for (int i = 0; i < n - substringSize + 1; i++) {
if (check(i, s)) {
answer.add(i);
}
}

return answer;
}
}

// class Solution {
// public List<Integer> findSubstring(String s, String[] words) {
// List<Integer> lst = new LinkedList();
// for(int i=0; i<s.length(); i+=4){
// int x = Check(s,words, i);
// if(x!=-1){
// lst.add(x);
// }
// }
// return lst;

// }
// int Check(String s,String[] words, int index){
// HashMap <String, Integer> map = new HashMap();
// for(int i=0; i<words.length; i++) {
// if(map.containsKey(words[i])){
// int x = map.get(words[i]);
// x++;
// map.put(words[i],x);
// }
// else{
// map.put(words[i],1);
// }
// }
// int i=0;
// while(i<words.length*words[0].length()){
// String str = "";
// int j=0;
// while(j<4&&i<words.length){
// str+=s.charAt(i+index);
// i++;
// j++;
// }
// if(map.containsKey(str)){
// int x = map.get(str);
// x--;
// if(x==0){
// map.remove(str);
// }
// }
// if(map.size()==0){
// return i+index-words.length*words[0].length();

// }

// }
// return -1;

// }

// }

0 comments on commit fe33211

Please sign in to comment.