Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Java/DynamicProgramming/EditDistance.java at master · ahaxuo/Java · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ahaxuo
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
Java
/
DynamicProgramming
/
EditDistance.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
80 lines (75 loc) · 2.75 KB
master
Breadcrumbs
Java
/
DynamicProgramming
/
EditDistance.java
Copy path
Top
File metadata and controls
Code
Blame
80 lines (75 loc) · 2.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package
DynamicProgramming
;
/**
* A DynamicProgramming based solution for Edit Distance problem In Java Description of Edit
* Distance with an Example:
*
* <p>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. The distance operations are the removal, insertion, or substitution of a character in the
* string.
*
* <p>
*
* <p>The Distance between "kitten" and "sitting" is 3. A minimal edit script that transforms the
* former into the latter is:
*
* <p>kitten → sitten (substitution of "s" for "k") sitten → sittin (substitution of "i" for "e")
* sittin → sitting (insertion of "g" at the end).
*
* @author SUBHAM SANGHAI
*/
import
java
.
util
.
Scanner
;
public
class
EditDistance
{
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
];
/* If second string is empty, the only option is to
insert all characters of first string into second*/
for
(
int
i
=
0
;
i
<=
len1
;
i
++) {
dp
[
i
][
0
] =
i
;
}
/* If first string is empty, the only option is to
insert all characters of second string into first*/
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
{
/* if two characters are different ,
then take the minimum of the various operations(i.e insertion,removal,substitution)*/
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 the final answer , after traversing through both the strings*/
return
dp
[
len1
][
len2
];
}
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
=
minDistance
(
s1
,
s2
);
System
.
out
.
println
(
"The minimum Edit Distance between
\"
"
+
s1
+
"
\"
and
\"
"
+
s2
+
"
\"
is "
+
ans
);
input
.
close
();
}
}
You can’t perform that action at this time.