Skip to content

Commit b7b9443

Browse files
dynamitechetansanghaisubham
authored andcommitted
Merge pull request TheAlgorithms#137 from daniel-mueller/coding-style-fixes
turned some public methods private
2 parents ef1aa79 + 3f25ae8 commit b7b9443

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
Author : SUBHAM SANGHAI
3+
A Dynamic Programming based solution for Edit Distance problem In Java
4+
Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another,
5+
by counting the minimum number of operations required to transform one string into the other
6+
**/
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.Scanner;
10+
public class Edit_Distance
11+
{
12+
public static int minDistance(String word1, String word2)
13+
{
14+
int len1 = word1.length();
15+
int len2 = word2.length();
16+
// len1+1, len2+1, because finally return dp[len1][len2]
17+
int[][] dp = new int[len1 + 1][len2 + 1];
18+
19+
for (int i = 0; i <= len1; i++)
20+
{
21+
dp[i][0] = i;
22+
}
23+
24+
for (int j = 0; j <= len2; j++)
25+
{
26+
dp[0][j] = j;
27+
}
28+
//iterate though, and check last char
29+
for (int i = 0; i < len1; i++)
30+
{
31+
char c1 = word1.charAt(i);
32+
for (int j = 0; j < len2; j++)
33+
{
34+
char c2 = word2.charAt(j);
35+
//if last two chars equal
36+
if (c1 == c2)
37+
{
38+
//update dp value for +1 length
39+
dp[i + 1][j + 1] = dp[i][j];
40+
}
41+
else
42+
{
43+
int replace = dp[i][j] + 1;
44+
int insert = dp[i][j + 1] + 1;
45+
int delete = dp[i + 1][j] + 1;
46+
47+
int min = replace > insert ? insert : replace;
48+
min = delete > min ? min : delete;
49+
dp[i + 1][j + 1] = min;
50+
}
51+
}
52+
}
53+
return dp[len1][len2];
54+
}
55+
// Driver program to test above function
56+
public static void main(String args[])
57+
{
58+
Scanner input = new Scanner(System.in);
59+
String s1,s2;
60+
System.out.println("Enter the First String");
61+
s1 = input.nextLine();
62+
System.out.println("Enter the Second String");
63+
s2 = input.nextLine();
64+
//ans stores the final Edit Distance between the two strings
65+
int ans=0;
66+
ans=minDistance(s1,s2);
67+
System.out.println("The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 +"\" is "+ans);
68+
}
69+
}

Dynamic Programming/Fibonacci.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class Fibonacci {
1313

14-
public static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
14+
private static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
1515

1616
public static void main(String[] args) throws Exception {
1717

@@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
2929
* Outputs the nth fibonacci number
3030
**/
3131

32-
public static int fibMemo(int n) {
32+
private static int fibMemo(int n) {
3333
if (map.containsKey(n)) {
3434
return map.get(n);
3535
}
@@ -54,7 +54,7 @@ public static int fibMemo(int n) {
5454
* Outputs the nth fibonacci number
5555
**/
5656

57-
public static int fibBotUp(int n) {
57+
private static int fibBotUp(int n) {
5858

5959
Map<Integer,Integer> fib = new HashMap<Integer,Integer>();
6060

Dynamic Programming/Levenshtein_distance.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
public class Levenshtein_distance{
10-
private int minimum(int a, int b, int c){
10+
private static int minimum(int a, int b, int c){
1111
if(a < b && a < c){
1212
return a;
1313
}else if(b < a && b < c){
@@ -16,7 +16,7 @@ private int minimum(int a, int b, int c){
1616
return c;
1717
}
1818
}
19-
public int calculate_distance(String a, String b){
19+
private static int calculate_distance(String a, String b){
2020
len_a = a.length() + 1;
2121
len_b = b.length() + 1;
2222
int [][] distance_mat = new int[len_a][len_b];

Dynamic Programming/LongestIncreasingSubsequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private static int upperBound(int[] ar, int l, int r, int key) {
3131
return r;
3232
}
3333

34-
public static int LIS(int[] array) {
34+
private static int LIS(int[] array) {
3535
int N = array.length;
3636
if (N == 0)
3737
return 0;

0 commit comments

Comments
 (0)