Code point
Avoid using char
. That type is essentially broken since Java 2, legacy since Java 5. As a 16-bit value, char
is physically incapable of representing most characters. One emoji could ruin your whole day.
Instead, use code point integer numbers when working with individual characters. You’ll find code point related methods on several classes, including String
, StringBuilder
, and Character
.
int[] codePointsX = x.codePoints().toArray() ;
int[] codePointsY = y.codePoints().toArray() ;
First, check lengths.
boolean sameLength = ( codePointsX.length == codePointsY.length ) ;
if ( ! sameLength ) { … }
Create an array to hold results of comparing each character.
int[] results = new int[ codePointsX.length ] ;
Compare. The ternary operator ?:
is a condensed alternative to using an if
test.
We put a zero into results array if the two code points match. Else, we put a zero into results array. The default is a zero, so we could skip the assignment of zero. But I want our intentions to be crystal clear to the reader.
for( int index = 0 ; index < codePointsX.length ; index ++ )
{
results[ index ] =
codePointsX[ index ] == codePointsY[ index ] ? 1 : 0 ; // One for a matching character, Zero for a mismatch.
}
Results.
int countMatches = Arrays.stream( results ).sum();
Full example code.
String x = "cat" ;
String y = "cot" ;
int[] codePointsX = x.codePoints().toArray() ;
int[] codePointsY = y.codePoints().toArray() ;
boolean sameLength = ( codePointsX.length == codePointsY.length ) ;
if ( ! sameLength ) {
throw new IllegalArgumentException( "The two input strings have different lengths. " ) ;
}
int[] results = new int[ codePointsX.length ] ;
for( int index = 0 ; index < codePointsX.length ; index ++ )
{
results[ index ] =
codePointsX[ index ] == codePointsY[ index ] ? 1 : 0 ;
}
int countMatches = Arrays.stream( results ).sum();
System.out.println( Arrays.toString( results ) ) ;
System.out.println( "countMatches = " + countMatches ) ;
See this code run at Ideone.com.
[1, 0, 1]
countMatches = 2