1010import com .microsoft .azure .toolkit .lib .common .form .AzureValidationInfo ;
1111import org .apache .commons .lang3 .CharUtils ;
1212import org .apache .commons .lang3 .StringUtils ;
13- import org .apache .commons .text .similarity .LongestCommonSubsequence ;
1413
1514import javax .swing .*;
1615import java .util .Arrays ;
@@ -73,11 +72,10 @@ private static AzureValidationInfo validatePassword(String password, String user
7372 .type (AzureValidationInfo .Type .ERROR ).build ();
7473 }
7574 // validate longest common subsequence between username and password.
76- LongestCommonSubsequence algorithm = new LongestCommonSubsequence ();
77- int longestCommonSubsequenceLength = algorithm .apply (username , password );
75+ int longestCommonSubstringLength = longestCommonSubstringLength (username , password );
7876 int usernameLength = StringUtils .length (username );
79- if ((usernameLength > 0 && longestCommonSubsequenceLength == usernameLength )
80- || longestCommonSubsequenceLength >= LONGEST_COMMON_SUBSEQUENCE_BETWEEN_NAME_AND_PASSWORD ) {
77+ if ((usernameLength > 0 && longestCommonSubstringLength == usernameLength )
78+ || longestCommonSubstringLength >= LONGEST_COMMON_SUBSEQUENCE_BETWEEN_NAME_AND_PASSWORD ) {
8179 final AzureValidationInfo .AzureValidationInfoBuilder builder = AzureValidationInfo .builder ();
8280 return builder .input (input ).message ("Your password cannot contain all or part of the login name." +
8381 " Part of a login name is defined as three or more consecutive alphanumeric characters." )
@@ -86,6 +84,25 @@ private static AzureValidationInfo validatePassword(String password, String user
8684 return AzureValidationInfo .OK ;
8785 }
8886
87+ private static int longestCommonSubstringLength (String left , String right ) {
88+ if (StringUtils .isAnyBlank (left , right )) {
89+ return 0 ;
90+ }
91+ int max = 0 ;
92+ final int [][] dp = new int [left .length () + 1 ][right .length () + 1 ];
93+ for (int i = 0 ; i < left .length (); i ++) {
94+ for (int j = 0 ; j < right .length (); j ++) {
95+ if (left .charAt (i ) == right .charAt (j )) {
96+ dp [i + 1 ][j + 1 ] = dp [i ][j ] + 1 ;
97+ } else {
98+ dp [i + 1 ][j + 1 ] = 0 ;
99+ }
100+ max = Math .max (max , dp [i + 1 ][j + 1 ]);
101+ }
102+ }
103+ return max ;
104+ }
105+
89106 private static int countCharacterCategories (final String value ) {
90107 final Boolean [] categories = new Boolean [] {false , false , false , false };
91108 for (char ch : value .toCharArray ()) {
0 commit comments