Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions day10/Java/permutations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @author: chaitanya-bhojwani
* @date: 01-01-2019
**/
import java.util.*;
class permutations{
public static void main(String args[]){
System.out.println("---------Daily Codes - Day 10 ---------------");
System.out.println("---String Permutation Problem---");
System.out.println("Enter the string for permutations: ");
Scanner sc = new Scanner(System.in);
String input = sc.next();
char[] in = input.toCharArray();
System.out.println("Possible permutations of String are: ");
generator("",in);
}
static void generator(String prev, char[] in){
char[] out = new char[in.length];
for(int i=0;i<in.length;i++){
out[0] = in[i];
int k=1;
for(int j=0;j<in.length;j++){
if(out[0]!=in[j]){
out[k] = in[j];
k++;
}
}
if(out.length>3){
generator(prev+String.valueOf(out[0]),Arrays.copyOfRange(out, 1, out.length));
}
else{
print_permutation(prev,out);
}
}
}
static void print_permutation(String prev, char[] permute){
System.out.println(prev+""+String.valueOf(permute));
char[] previous = new char[permute.length];
System.arraycopy(permute,0,previous,0,permute.length);
for(int p=0;p<permute.length-2;p++){
char[] next = new char[previous.length];
next[0] = permute[0];
for(int q=1;q<permute.length;q++){
if(q==1){
next[q] = previous[previous.length-1];
}
else{
next[q] = previous[q-1];
}
}
System.out.println(prev+""+String.valueOf(next));
System.arraycopy(next,0,previous,0,next.length);
}
}
}
63 changes: 63 additions & 0 deletions day11/Java/longest_common_substring.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @author: chaitanya-bhojwani
* @date: 03-01-2019
**/
import java.util.*;
class longest_common_substring{
public static void main(String args[]){
System.out.println("---------Daily Codes - Day 11 ---------------");
System.out.println("---Longest Common Substring Problem---");
System.out.println("Enter the first string: ");
Scanner sc = new Scanner(System.in);
String first = sc.next();
System.out.println("Enter the second string: ");
String second = sc.next();
int maxval = 0;
int maxposi = 0;
int maxposj = 0;
String result = "";
int[][] mat = new int[first.length()][second.length()];
for(int i=0;i<first.length();i++){
for(int j=0;j<second.length();j++){
if(first.charAt(i)==second.charAt(j)){
if(i==0||j==0){
mat[i][j] = 1;
}
else{
mat[i][j] = mat[i-1][j-1] + 1;
}
if(mat[i][j]>maxval){
maxval = mat[i][j];
maxposi = i;
maxposj = j;
}
}
else{
mat[i][j] = 0;
}
}
}
if(maxval>0){
int i=0;
while(true){
if(maxposi-i>=0 && maxposj-i>=0){
if(mat[maxposi-i][maxposj-i]>0){
result = result + String.valueOf(first.charAt(maxposi-i));
i++;
}
else{
break;
}
}
else{
break;
}
}
System.out.println("Longest Common Substring is: ");
System.out.println(new StringBuffer(result).reverse().toString());
}
else{
System.out.println("No Common Substring Found");
}
}
}