Java to find the length of the longest common substring

Source: Internet
Author: User

1 The topic is to give two strings, and then to find the maximum length of the longest common substring of the two strings, assuming my f () method is to find the largest common substring of two strings, compare them from the beginning, and if they are equal,

Continue to call this method, so that the length of the recursion +1, if not equal, as long as the comparison S1 truncated one and S2 comparison, and S2 truncation and S1 comparison, the largest of two, if S1 or S2 has a length of 0, then the maximum public length is 0,return

2. code example:

Package Zzl;public class Longest common substring {public static void main (string[] args) {int a = f ("ABCD", "xacdb"); System.out.println (a);} private static int F (string s1, string s2) {if (S1.length () ==0 | | s2.length () ==0) {//s1 or S2 has one for 0return 0;} if (S1.charat (0) = = S2.charat (0)) {return F (s1.substring (1), s2.substring (1)) +1;//Take the next}else {return Math.max (f ( S1.substring (1), S2), F (s1,s2.substring (1)));  Compare two Larger}}}

  

Java to find the length of the longest common substring

Contact Us

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.