Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Created September 9, 2014 14:26
Show Gist options
  • Save sharmaabhinav/a93b64c5680703b686e1 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/a93b64c5680703b686e1 to your computer and use it in GitHub Desktop.
// 0 means there is no substring
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
String s1 = "dyittvbiaxyavvvyia";
String s2 = "itya";
String s3 = "z";
String s4 = "xz";
System.out.println(returnShortestLength(s1, s2, s3));
System.out.println(returnShortestLength(s1, s2, s4));
}
static int returnShortestLength(String s1, String s2, String s3){
int s1_char_count[] = new int[26];
if (s2.length() > s1.length()){
return 0;
}
int k1 = s1.length();
int k2 = s2.length();
int i , j;
while(k2 <= k1){
i = 0;
j = 0;
while(j < k2){
s1_char_count[s1.charAt(j) - 97] += 1;
if (checkArray(s1_char_count, s2, s3) == true){
return k2;
}
j++;
}
while(j < k1){
s1_char_count[s1.charAt(j) - 97] += 1;
s1_char_count[s1.charAt(i) - 97] -= 1;
if (checkArray(s1_char_count, s2, s3) == true){
return k2;
}
j++;
i++;
}
clearCountArray(s1_char_count);
k2++;
}
return 0;
}
static boolean checkArray(int arr[], String s2, String s3 ){
boolean s2_ = true;
boolean s3_ = true;
int i = 0;
while( i < s2.length()){
if (arr[s2.charAt(i) - 97] == 0){
s2_ = false;
}
i++;
}
i = 0;
while( i < s3.length()){
if (arr[s3.charAt(i) - 97] != 0){
s3_ = false;
}
i++;
}
return s2_ && s3_;
}
static void clearCountArray(int arr[]){
for(int i=0;i<=25;i++){
arr[i] = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment