Skip to content

Commit abf05b0

Browse files
chaitanya-bhojwaniMadhavBahl
authored andcommitted
Add Day 10 & Day 11 - Java Implementation (#116)
* Update @chaitanya-bhojwani as a contributor * Java Implementation * Add day 10 and day 11
1 parent 9080b91 commit abf05b0

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

day10/Java/permutations.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @author: chaitanya-bhojwani
3+
* @date: 01-01-2019
4+
**/
5+
import java.util.*;
6+
class permutations{
7+
public static void main(String args[]){
8+
System.out.println("---------Daily Codes - Day 10 ---------------");
9+
System.out.println("---String Permutation Problem---");
10+
System.out.println("Enter the string for permutations: ");
11+
Scanner sc = new Scanner(System.in);
12+
String input = sc.next();
13+
char[] in = input.toCharArray();
14+
System.out.println("Possible permutations of String are: ");
15+
generator("",in);
16+
}
17+
static void generator(String prev, char[] in){
18+
char[] out = new char[in.length];
19+
for(int i=0;i<in.length;i++){
20+
out[0] = in[i];
21+
int k=1;
22+
for(int j=0;j<in.length;j++){
23+
if(out[0]!=in[j]){
24+
out[k] = in[j];
25+
k++;
26+
}
27+
}
28+
if(out.length>3){
29+
generator(prev+String.valueOf(out[0]),Arrays.copyOfRange(out, 1, out.length));
30+
}
31+
else{
32+
print_permutation(prev,out);
33+
}
34+
}
35+
}
36+
static void print_permutation(String prev, char[] permute){
37+
System.out.println(prev+""+String.valueOf(permute));
38+
char[] previous = new char[permute.length];
39+
System.arraycopy(permute,0,previous,0,permute.length);
40+
for(int p=0;p<permute.length-2;p++){
41+
char[] next = new char[previous.length];
42+
next[0] = permute[0];
43+
for(int q=1;q<permute.length;q++){
44+
if(q==1){
45+
next[q] = previous[previous.length-1];
46+
}
47+
else{
48+
next[q] = previous[q-1];
49+
}
50+
}
51+
System.out.println(prev+""+String.valueOf(next));
52+
System.arraycopy(next,0,previous,0,next.length);
53+
}
54+
}
55+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @author: chaitanya-bhojwani
3+
* @date: 03-01-2019
4+
**/
5+
import java.util.*;
6+
class longest_common_substring{
7+
public static void main(String args[]){
8+
System.out.println("---------Daily Codes - Day 11 ---------------");
9+
System.out.println("---Longest Common Substring Problem---");
10+
System.out.println("Enter the first string: ");
11+
Scanner sc = new Scanner(System.in);
12+
String first = sc.next();
13+
System.out.println("Enter the second string: ");
14+
String second = sc.next();
15+
int maxval = 0;
16+
int maxposi = 0;
17+
int maxposj = 0;
18+
String result = "";
19+
int[][] mat = new int[first.length()][second.length()];
20+
for(int i=0;i<first.length();i++){
21+
for(int j=0;j<second.length();j++){
22+
if(first.charAt(i)==second.charAt(j)){
23+
if(i==0||j==0){
24+
mat[i][j] = 1;
25+
}
26+
else{
27+
mat[i][j] = mat[i-1][j-1] + 1;
28+
}
29+
if(mat[i][j]>maxval){
30+
maxval = mat[i][j];
31+
maxposi = i;
32+
maxposj = j;
33+
}
34+
}
35+
else{
36+
mat[i][j] = 0;
37+
}
38+
}
39+
}
40+
if(maxval>0){
41+
int i=0;
42+
while(true){
43+
if(maxposi-i>=0 && maxposj-i>=0){
44+
if(mat[maxposi-i][maxposj-i]>0){
45+
result = result + String.valueOf(first.charAt(maxposi-i));
46+
i++;
47+
}
48+
else{
49+
break;
50+
}
51+
}
52+
else{
53+
break;
54+
}
55+
}
56+
System.out.println("Longest Common Substring is: ");
57+
System.out.println(new StringBuffer(result).reverse().toString());
58+
}
59+
else{
60+
System.out.println("No Common Substring Found");
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)