Min Window Substring Coderbyte Solution

0

Title: Min Window Substring

Difficulty: Medium

Maximum Score: 10

Description: For this challenge you will be searching for the optimal substring of k characters.

Have the function MinWindowSubstring(strArr) take the array of strings stored in strArr, which will contain only two strings, the first parameter being the string N and the second parameter being a string K of some characters, and your goal is to determine the smallest substring of N that contains all the characters in K. For example: if strArr is ["aaabaaddae", "aed"] then the smallest substring of N that contains the characters a, e, and d is "dae" located at the end of the string. So for this example your program should return the string dae.

Another example: if strArr is ["aabdccdbcacd", "aad"] then the smallest substring of N that contains all of the characters in K is "aabd" which is located at the beginning of the string. Both parameters will be strings ranging in length from 1 to 50 characters and all of K's characters will exist somewhere in the string N. Both strings will only contains lowercase alphabetic characters. 

Example 1


    Input: new String[] {"ahffaksfajeeubsne", "jefaa"}
    Output: aksfaje

Example 2 


    Input: new String[] {"aaffhkksemckelloe", "fhea"}
    Output: affhkkse


Min Window Substring Solution 


 import java.util.*;
 import java.io.*;

 class Main {

  public static String MinWindowSubstring(String[] strArr) {
   String N = strArr[0];
   String K = strArr[1];
   int min = Integer.MAX_VALUE;
   String result = "";
   for (int i = 0; i < N.length(); i++) {
     StringBuilder match = new StringBuilder(K)
     for (int j=i; j < N.length(); j++) {
       if (match.toString().contains(String.valueOf(N.charAt(j)))) {
        int index = match.indexOf(String.valueOf(N.charAt(j)));
        match.replace(index, index + 1, "");
       }

      if (match.length() == 0) {
       if (j - i < min) {
         min = j - i;
         result = N.substring(i, j+1);
       }
       break;
     }
    }
 }
    return result;
 }

 public static void main (String[] args) {
 // keep this function call here
   Scanner s = new Scanner(System.in);
   System.out.print(MinWindowSubstring(s.nextLine()));
 }

 }




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !