Skip to content

Commit 6701803

Browse files
SpreehaMadhavBahl
authored andcommitted
day9 (#102)
1 parent b8438c4 commit 6701803

File tree

3 files changed

+125
-3
lines changed

3 files changed

+125
-3
lines changed

day9/Java/minSubstring.java

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

day9/README.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,67 @@ console.log (smallestSubstr ("x", ['x']));
9292
console.log (smallestSubstr ("afekbtcodebancfeger", ["a","b","c"]));
9393
```
9494

95+
## Java Implementation
96+
97+
### [Solution](./Java/minSubstring.java)
98+
99+
```java
100+
/**
101+
* @date 01/01/19
102+
* @author SPREEHA DUTTA
103+
*/
104+
import java.util.*;
105+
public class minSubstring {
106+
107+
public static boolean check (String s,char []c)
108+
{
109+
String str=new String(c);int k=0;
110+
for(int i=0;i<c.length;i++)
111+
{
112+
if(s.contains(str.substring(i,i+1)))
113+
k++;
114+
}
115+
if(k==c.length)
116+
return true;
117+
else
118+
return false;
119+
}
120+
121+
public static void generate(String str,char c[]) {
122+
String s;int min;
123+
min=str.length();String s1="";
124+
for(int i=0;i<=str.length()-1;i++)
125+
{
126+
for(int j=i+1;j<=str.length();j++)
127+
{
128+
s=str.substring(i,j);
129+
if(check(s,c)==true)
130+
{
131+
if(min>=s.length())
132+
{
133+
min=s.length();
134+
s1=s;
135+
}
136+
}
137+
}
138+
}
139+
System.out.println(s1);
140+
}
141+
142+
public static void main(String[] args) {
143+
Scanner sc=new Scanner(System.in);
144+
String s;int i,n;
145+
System.out.println("Enter a string");
146+
s=sc.next();
147+
System.out.println("Enter size of character array");
148+
n=sc.nextInt();
149+
char a[]=new char[n];
150+
for(i=0;i<n;i++)
151+
a[i]=sc.next().charAt(0);
152+
generate(s,a);
153+
}
154+
}
155+
```
95156

96157
## C++Implementation
97158

@@ -170,9 +231,6 @@ int main() {
170231
}
171232
```
172233

173-
174-
175-
176234
### Have Another solution?
177235

178236
The beauty of programming lies in the fact that there is never a single solution to any problem.

day9/dailycodebase

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit e95480ab4bde1c11326b1a5b925ec0447e821dbb

0 commit comments

Comments
 (0)