-
Notifications
You must be signed in to change notification settings - Fork 153
/
minSubstring.java
63 lines (58 loc) · 1.56 KB
/
minSubstring.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;
/**
* @date 01/01/19
* @author SPREEHA DUTTA
*/
import java.util.*;
public class minSubstring {
public static boolean check (String s,char []c)
{
String str=new String(c);int k=0;
for(int i=0;i<c.length;i++)
{
if(s.contains(str.substring(i,i+1)))
k++;
}
if(k==c.length)
return true;
else
return false;
}
public static void generate(String str,char c[]) {
String s;int min;
min=str.length();String s1="";
for(int i=0;i<=str.length()-1;i++)
{
for(int j=i+1;j<=str.length();j++)
{
s=str.substring(i,j);
if(check(s,c)==true)
{
if(min>=s.length())
{
min=s.length();
s1=s;
}
}
}
}
System.out.println(s1);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s;int i,n;
System.out.println("Enter a string");
s=sc.next();
System.out.println("Enter size of character array");
n=sc.nextInt();
char a[]=new char[n];
for(i=0;i<n;i++)
a[i]=sc.next().charAt(0);
generate(s,a);
}
}