# String similarity (implemented in java)

Source: Internet
Author: User
`Public class test {/*** we define the similarity between two strings as the cost of converting a string into another string (the conversion method may not be unique ), the higher the conversion cost, the lower the similarity between the two strings. For example, two strings: "SNOWY" and "SUNNY". Below are two methods to convert "SNOWY" to "SUNNY": 1: s-n o w y s u n-Y Cost = 3 (insert U, replace O, delete W) transform 2: -s n o w-y s u n-n y Cost = 5 (insert S, replace S, delete O, delete W, insert N) use d [I, j] indicates source [1 .. i] to target [1 .. if the source [I] is equal to target [j], then the recursive relationship of d [I, j] can be calculated as follows: d [I, j] = d [I, j] + 0 (recurrence 1) If source [I] is not equal to target [j], based on the insert, delete, and replace policies, the distance between the three policies is calculated, and the smallest one is: d [I, j] = min (d [I, j, j-1] + 1, d [I-1, j] + 1, d [I-1, j-1] + 1) (recursive formula 2) d [I, j-1] + 1 indicates that the minimum editing distance d [I-1, j] + 1 indicates that the minimum editing distance d [I-1, j-1] + 1 indicates replacing source [I] with target [I], and then calculating the minimum editing distance * @ param args */public static void main (String [] args) {// TODO Auto-generated method stubString source = "SUNNY"; String target = "hgjdSUN"; int I = 0; // I = EditDistanceChange (source, target ); I = EditDistance (source, target); System. out. Println ("requires" + I + "times");} // Dynamic Programming private static int EditDistance (String source, String target) {char [] s = source. toCharArray (); char [] t = target. toCharArray (); int slen = source. length (); int tlen = target. length (); int d [] [] = new int [slen + 1] [tlen + 1]; for (int I = 0; I <= slen; I ++) {d [I] [0] = I;} for (int I = 0; I <= tlen; I ++) {d [0] [I] = I ;} for (int I = 1; I <= slen; I ++) {for (int j = 1; j <= tlen; j ++) {if (s [I-1] = t [J-1]) {d [I] [j] = d [I-1] [J-1];} els E {int insert = d [I] [J-1] + 1; int del = d [I-1] [j] + 1; int update = d [I-1] [J-1] + 1; d [I] [j] = Math. min (insert, del)> Math. min (del, update )? Math. min (del, update): Math. min (insert, del) ;}} return d [slen] [tlen] ;}// Recursive Implementation --- exhaustive method (enumeration method) private static int EditDistanceChange (String source, String target) {if (target. length ()! = 0 & source. length () = 0) {return EditDistanceChange (source, target. substring (1) + 1;} else if (target. length () = 0 & source. length ()! = 0) {return EditDistanceChange (source. substring (1), target) + 1;} else if (target. length ()! = 0 & source. length ()! = 0) {// when the first value of the source character is the same as the first value of the target character if (source. charAt (0) = target. charAt (0) {return EditDistanceChange (source. substring (1), target. substring (1);} else {int insert = EditDistanceChange (source. substring (1), target) + 1; int del = EditDistanceChange (source, target. substring (1) + 1; int update = EditDistanceChange (source. substring (1), target. substring (1) + 1; return Math. min (insert, del)> Math. min (del, update )? Math. min (del, update): Math. min (insert, del) ;}} else {return 0 ;}}}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.