fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String x = "cat" ;
  13. String y = "cot" ;
  14.  
  15. int[] codePointsX = x.codePoints().toArray() ;
  16. int[] codePointsY = y.codePoints().toArray() ;
  17.  
  18. boolean sameLength = ( codePointsX.length == codePointsY.length ) ;
  19. if ( ! sameLength ) {
  20. throw new IllegalArgumentException( "The two input strings have different lengths. " ) ;
  21. }
  22.  
  23. int[] results = new int[ codePointsX.length ] ;
  24.  
  25. for( int index = 0 ; index < codePointsX.length ; index ++ )
  26. {
  27. results[ index ] =
  28. codePointsX[ index ] == codePointsY[ index ] ? 1 : 0 ;
  29. }
  30.  
  31. int countMatches = Arrays.stream( results ).sum();
  32.  
  33. System.out.println( Arrays.toString( results ) ) ;
  34. System.out.println( "countMatches = " + countMatches ) ;
  35. }
  36. }
Success #stdin #stdout 0.18s 52840KB
stdin
Standard input is empty
stdout
[1, 0, 1]
countMatches = 2