Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Longest_Common_subsequence.java #143

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 43 additions & 0 deletions Longest_substring_without_repeating_characters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;

/**
*
* @author Sadiya Khatoon
*/
public class Longest_substring_without_repeating_characters {

/**
* @param args the command line arguments
*/
public int length(String s)
{ char c;
HashSet<Character> h=new HashSet<>();
int max=0;
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(!h.contains(c))
{
h.add(c);
}
else{
max=Math.max(max, h.size());
h.clear();

}
}
return max;
}
public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// TODO code application logic here
String s=br.readLine();
Longest_substring_without_repeating_characters ob=new Longest_substring_without_repeating_characters ();
System.out.print(ob.length(s));
}

}
76 changes: 76 additions & 0 deletions Min_path_sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author Sadiya Khatoon
*/
public class Min_path_sum {

/**
* @param grid
* @return
*/


public int min_path(int [][]grid)
{


//dynamic programming (bottom up approach)
if (grid==null || grid.length==0)
return 0;
int m=grid.length;
int n=grid[0].length;
int res[][]=new int[m][n];
res[m-1][n-1]=grid[m-1][n-1];//last cell
//populating values in the latst column
if(m>1)
{
for(int i=m-2;i>=0;i--)
{
res[i][n-1]=grid[i][n-1]+res[i+1][n-1];

}

}
//populating the values in the last row
if(n>1)
{
for (int j=n-2;j>=0;j--)
{
res[m-1][j]=res[m-1][j+1]+grid[m-1][j];
}
}
//rest of the cell
for(int i=m-2;i>=0;i--)
{
for(int j=n-2;j>=0;j--)
{
res[i][j]=grid[i][j]+Math.min(res[i+1][j],res[i][j+1]);
}
}

return(res[0][0]);
}


public static void main(String[] args)throws Exception {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int g[][]=new int[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
g[i][j]=Integer.parseInt(br.readLine());
}
}


Min_path_sum ob=new Min_path_sum ();
System.out.println(ob.min_path(g));
}

}
45 changes: 45 additions & 0 deletions Programs/Longest_Common_subsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author Sadiya Khatoon
*/
public class LCS {

/**
* @param s
* @param t
* @return
*/


public int lcss(String s,String t)
{
int op1,op2;
if(s.length()==0 || t.length()==0)
return 0;
if(s.charAt(0) == t.charAt(0))
return 1+ lcss(s.substring(1), t.substring(1));
else
{
op1=lcss(s,t.substring(1));
op2=lcss(s.substring(1),t);
return Math.max(op1, op2);

}


}
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// TODO code application logic here
String s=br.readLine();
String t=br.readLine();
LCS ob =new LCS();
int f=ob.lcss(s,t);
System.out.println(+f);
}

}
48 changes: 48 additions & 0 deletions Roman_to_integer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author Sadiya Khatoon
*/
public class Roman_to_integer {

/**
* @param args the command line arguments
*/
public int roman(String s)
{
Map <Character,Integer> hm=new HashMap<>();
hm.put('I',1);
hm.put('V',5);
hm.put('X',10);hm.put('L',50);hm.put('C',100);hm.put('D',500);
hm.put('N',1000);


int res =hm.get(s.charAt(s.length()-1));
for(int i=s.length()-2;i>=0;i--)
{
if(hm.get(s.charAt(i))<hm.get(s.charAt(i+1)))
{
res -=hm.get(s.charAt(i-1));
}
else res +=hm.get(s.charAt(i));
}
return res;

}
public static void main(String[] args)throws IOException{
// TODO code application logic here
Roman_to_integer ob=new Roman_to_integer();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter roman number using 'I' ,'V','X','L,'C','D' or 'N' only");
String s=br.readLine();

System.out.println(ob.roman(s));
}

}