String Window Hashing and tries

import java.util.*;
public class StringWindow {

public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	String string1 = "";
	string1 += sc.nextLine();
	String string2 = sc.next();
	HashSet<Character> set = new HashSet<>();
	int count = 0;
	for(int i=0;i<string2.length();i++) {
		set.add(string2.charAt(i));
	}
	int start=0;
	int end=0;
	for(int i=0;i<string1.length();i++) {
		if(string1.charAt(i)==' ') {
			start = 0;
			end = 0;
		}
		if(set.contains(string1.charAt(i))) {
			if(count>string2.length()) {
				break;
			}
			if(count==0) {
			 start = i;
			count++;
			}else {
				 end = i;
				 count++;
			}
		}
	}
	
	String ans =  string1.substring(start, end);
	System.out.println(ans);
}

}

//Sir I’m getting all the substrings except the one containing the string2 .

@Siddharth_sharma1808,

Don’t use a hashSet, instead use a hashMap of type Character and Integer.

Suggested approach:

  1. First check if length of string is less than the length of given pattern, if yes then "no such window can exist ".
  2. Store the occurrence of characters of given pattern in a hashMap.
  3. Start matching the characters of pattern with the characters of string i.e. increment count if a character matches
  4. Check if (count == length of pattern ) this means a window is found
  5. If such window found, try to minimize it by removing extra characters from beginning of current window.
  6. Update min_length
  7. Print the minimum length window.

@Siddharth_sharma1808,
https://ide.codingblocks.com/s/227098 corrected code.

For the second string, use the same method. The second string can also contain space
(" ").

And print “No String” if no match found.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.