Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 769cc5b

Browse files
Create Deletions_and_insertions.java
1 parent a53ec4c commit 769cc5b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//Minimum Number of Insertion and Deletion to convert from String 'a' to 'b'
2+
//Solution:
3+
4+
public class HelloWorld{
5+
6+
public static void main(String []args){
7+
String a = "peap";
8+
String b = "gep";
9+
//Converting string a to b.
10+
11+
int lena = a.length();
12+
int lenb = b.length();
13+
int i ,j;
14+
//Initialization.
15+
int[][] arr = new int[lena+1][lenb+1];
16+
for(i =0;i<=lena;i++)
17+
{
18+
for(j = 0;j<=lenb;j++)
19+
{
20+
if(i==0 || j ==0)
21+
arr[i][j] =0;
22+
}
23+
}
24+
for(i =1;i<=lena;i++)
25+
{
26+
for(j = 1;j<=lenb;j++)
27+
{
28+
if(a.charAt(i-1) == b.charAt(j-1))
29+
{
30+
arr[i][j] = 1 + arr[i-1][j-1];
31+
}
32+
else
33+
{
34+
arr[i][j] = Math.max(arr[i-1][j] , arr[i][j-1]);
35+
}
36+
37+
}
38+
}
39+
int common = arr[lena][lenb];
40+
int insertion = lenb- common;
41+
int deletion = lena - common ;
42+
System.out.println("To convert String from \na : "+a+" to b : "+b+" \nInsertion: "+insertion + "\nDeletion : "+ deletion + "\ntotal Insertion and Deletion are: "+ (insertion + deletion));
43+
//To print whole array.
44+
/*
45+
for(i =1;i<=lena;i++)
46+
{
47+
for(j = 1;j<=lenb;j++)
48+
{
49+
System.out.print(arr[i][j]+" ");
50+
}
51+
System.out.println("");
52+
}
53+
*/
54+
55+
}
56+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /