Merge pull request #137 from daniel-mueller/coding-style-fixes
turned some public methods private
This commit is contained in:
commit
b7b9443a4d
69
Dynamic Programming/Edit_Distance.java
Normal file
69
Dynamic Programming/Edit_Distance.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/**
|
||||||
|
Author : SUBHAM SANGHAI
|
||||||
|
A Dynamic Programming based solution for Edit Distance problem In Java
|
||||||
|
Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another,
|
||||||
|
by counting the minimum number of operations required to transform one string into the other
|
||||||
|
**/
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Scanner;
|
||||||
|
public class Edit_Distance
|
||||||
|
{
|
||||||
|
public static int minDistance(String word1, String word2)
|
||||||
|
{
|
||||||
|
int len1 = word1.length();
|
||||||
|
int len2 = word2.length();
|
||||||
|
// len1+1, len2+1, because finally return dp[len1][len2]
|
||||||
|
int[][] dp = new int[len1 + 1][len2 + 1];
|
||||||
|
|
||||||
|
for (int i = 0; i <= len1; i++)
|
||||||
|
{
|
||||||
|
dp[i][0] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j <= len2; j++)
|
||||||
|
{
|
||||||
|
dp[0][j] = j;
|
||||||
|
}
|
||||||
|
//iterate though, and check last char
|
||||||
|
for (int i = 0; i < len1; i++)
|
||||||
|
{
|
||||||
|
char c1 = word1.charAt(i);
|
||||||
|
for (int j = 0; j < len2; j++)
|
||||||
|
{
|
||||||
|
char c2 = word2.charAt(j);
|
||||||
|
//if last two chars equal
|
||||||
|
if (c1 == c2)
|
||||||
|
{
|
||||||
|
//update dp value for +1 length
|
||||||
|
dp[i + 1][j + 1] = dp[i][j];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int replace = dp[i][j] + 1;
|
||||||
|
int insert = dp[i][j + 1] + 1;
|
||||||
|
int delete = dp[i + 1][j] + 1;
|
||||||
|
|
||||||
|
int min = replace > insert ? insert : replace;
|
||||||
|
min = delete > min ? min : delete;
|
||||||
|
dp[i + 1][j + 1] = min;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[len1][len2];
|
||||||
|
}
|
||||||
|
// Driver program to test above function
|
||||||
|
public static void main(String args[])
|
||||||
|
{
|
||||||
|
Scanner input = new Scanner(System.in);
|
||||||
|
String s1,s2;
|
||||||
|
System.out.println("Enter the First String");
|
||||||
|
s1 = input.nextLine();
|
||||||
|
System.out.println("Enter the Second String");
|
||||||
|
s2 = input.nextLine();
|
||||||
|
//ans stores the final Edit Distance between the two strings
|
||||||
|
int ans=0;
|
||||||
|
ans=minDistance(s1,s2);
|
||||||
|
System.out.println("The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 +"\" is "+ans);
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class Fibonacci {
|
public class Fibonacci {
|
||||||
|
|
||||||
public static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
|
private static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ public class Fibonacci {
|
|||||||
* Outputs the nth fibonacci number
|
* Outputs the nth fibonacci number
|
||||||
**/
|
**/
|
||||||
|
|
||||||
public static int fibMemo(int n) {
|
private static int fibMemo(int n) {
|
||||||
if (map.containsKey(n)) {
|
if (map.containsKey(n)) {
|
||||||
return map.get(n);
|
return map.get(n);
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ public class Fibonacci {
|
|||||||
* Outputs the nth fibonacci number
|
* Outputs the nth fibonacci number
|
||||||
**/
|
**/
|
||||||
|
|
||||||
public static int fibBotUp(int n) {
|
private static int fibBotUp(int n) {
|
||||||
|
|
||||||
Map<Integer,Integer> fib = new HashMap<Integer,Integer>();
|
Map<Integer,Integer> fib = new HashMap<Integer,Integer>();
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public class Levenshtein_distance{
|
public class Levenshtein_distance{
|
||||||
private int minimum(int a, int b, int c){
|
private static int minimum(int a, int b, int c){
|
||||||
if(a < b && a < c){
|
if(a < b && a < c){
|
||||||
return a;
|
return a;
|
||||||
}else if(b < a && b < c){
|
}else if(b < a && b < c){
|
||||||
@ -16,7 +16,7 @@ public class Levenshtein_distance{
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public int calculate_distance(String a, String b){
|
private static int calculate_distance(String a, String b){
|
||||||
len_a = a.length() + 1;
|
len_a = a.length() + 1;
|
||||||
len_b = b.length() + 1;
|
len_b = b.length() + 1;
|
||||||
int [][] distance_mat = new int[len_a][len_b];
|
int [][] distance_mat = new int[len_a][len_b];
|
||||||
|
@ -31,7 +31,7 @@ public class LongestIncreasingSubsequence {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int LIS(int[] array) {
|
private static int LIS(int[] array) {
|
||||||
int N = array.length;
|
int N = array.length;
|
||||||
if (N == 0)
|
if (N == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user