Java Array Common Prefix longestCommonSuffix(final byte[] seq1, final byte[] seq2, final int maxLength)

Here you can find the source of longestCommonSuffix(final byte[] seq1, final byte[] seq2, final int maxLength)

Description

Get the length of the longest common suffix of seq1 and seq2

License

Open Source License

Parameter

Parameter Description
seq1 non-null byte array
seq2 non-null byte array
maxLength the maximum allowed length to return

Return

the length of the longest common suffix of seq1 and seq2, >= 0

Declaration

public static int longestCommonSuffix(final byte[] seq1, final byte[] seq2, final int maxLength) 

Method Source Code

//package com.java2s;
/*//from   ww  w  . j ava 2s.  c o  m
* Copyright 2012-2016 Broad Institute, Inc.
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

public class Main {
    /**
     * Get the length of the longest common suffix of seq1 and seq2
     * @param seq1 non-null byte array
     * @param seq2 non-null byte array
     * @param maxLength the maximum allowed length to return
     * @return the length of the longest common suffix of seq1 and seq2, >= 0
     */
    public static int longestCommonSuffix(final byte[] seq1, final byte[] seq2, final int maxLength) {
        if (seq1 == null)
            throw new IllegalArgumentException("seq1 is null");
        if (seq2 == null)
            throw new IllegalArgumentException("seq2 is null");
        if (maxLength < 0)
            throw new IllegalArgumentException("maxLength < 0 " + maxLength);

        final int end = Math.min(seq1.length, Math.min(seq2.length, maxLength));
        for (int i = 0; i < end; i++) {
            if (seq1[seq1.length - i - 1] != seq2[seq2.length - i - 1])
                return i;
        }
        return end;
    }
}

Related

  1. longestCommonPrefix4(String[] strs)
  2. longestCommonSequence(String str1, String str2)
  3. longestCommonSubstr(String s1, String s2)
  4. LongestCommonSubString(String firstString, String secondString)
  5. longestCommonSubstring(String S1, String S2)