Subsequences and longest common substrings of java interview algorithm questions

Source: Internet
Author: User


LCS (Longest Common Subsquence/String)

Subsequence
Substring
Eldest son sequence

The code is as follows: Copy code

Public static int getLongestCommonSubsequence (String a, String B ){
Int m = a. length ();
Int n = B. length ();
Int [] [] dp = new int [m + 1] [n + 1];

For (int I = 0; I <= m; I ++ ){
For (int j = 0; j <= n; j ++ ){
If (I = 0 | j = 0 ){
Dp [I] [j] = 0;
} Else if (a. charAt (i-1) = B. charAt (j-1 )){
Dp [I] [j] = 1 + dp [i-1] [j-1];
} Else {
Dp [I] [j] = Math. max (dp [i-1] [j], dp [I] [j-1]);
            }
        }
    }

Return dp [m] [n];
}

Eldest son string

Maximum output length

The code is as follows: Copy code

Public static int longestSubstr (String first, String second ){
If (first = null | second = null | first. length () = 0
| Second. length () = 0 ){
Return 0;
        }

Int maxLen = 0;
Int fl = first. length ();
Int sl = second. length ();
Int [] [] table = new int [fl + 1] [sl + 1];

For (int I = 1; I <= fl; I ++ ){
For (int j = 1; j <= sl; j ++ ){
If (first. charAt (I-1) = second. charAt (j-1 )){
If (I = 1 | j = 1 ){
Table [I] [j] = 1;
} Else {
Table [I] [j] = table [I-1] [j-1] + 1;
                    }
If (table [I] [j]> maxLen ){
MaxLen = table [I] [j];
                    }
                }
            }
        }
Return maxLen;
    }

Output the longest string

The code is as follows: Copy code

Public static String longestSubstring (String str1, String str2 ){
StringBuilder sb = new StringBuilder ();
If (str1 = null | str1.isEmpty () | str2 = null | str2.isEmpty ())
Return "";

// Ignore case
Str1 = str1.toLowerCase ();
Str2 = str2.toLowerCase ();

// Java initializes them already with 0
Int [] [] num = new int [str1.length ()] [str2.length ()];
Int maxlen = 0;
Int lastSubsBegin = 0;

For (int I = 0; I <str1.length (); I ++ ){
For (int j = 0; j <str2.length (); j ++ ){
If (str1.charAt (I) = str2.charAt (j )){
If (I = 0) | (j = 0 ))
Num [I] [j] = 1;
Else
Num [I] [j] = 1 + num [I-1] [j-1];

If (num [I] [j]> maxlen ){
Maxlen = num [I] [j];
// Generate substring from str1 => I
Int thisSubsBegin = I-num [I] [j] + 1;
If (lastSubsBegin = thisSubsBegin ){
// If the current LCS is the same as the last time
// This block ran
Sb. append (str1.charAt (I ));
} Else {
// This block resets the string builder if
// Different LCS is found
LastSubsBegin = thisSubsBegin;
Sb = new StringBuilder ();
Sb. append (str1.substring (lastSubsBegin, I + 1 ));
                        }
                    }
                }
            }
        }

Return sb. toString ();
    }

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.