fork download
  1. class AmstringBruteForce {
  2.  
  3. public static void main( final String[] args ) {
  4. printAll( 2, "abbab", "aabba" );
  5. }
  6.  
  7. private static void printAll( final int d, final String s1, final String s2 ) {
  8. for ( int len = 1; len <= s1.length(); len++ ) {
  9. for ( int s1s = 0; s1s <= s1.length() - len; s1s++ ) {
  10. for ( int s2s = 0; s2s <= s2.length() - len; s2s++ ) {
  11. final String s1sub = s1.substring( s1s, s1s + len );
  12. final String s2sub = s2.substring( s2s, s2s + len );
  13. final int hd = getHammingDistance( s1sub, s2sub );
  14. if ( hd <= d ) {
  15. System.out.printf( "%s (%d), %s (%d) - %d\n", s1sub, s1s, s2sub, s2s, hd );
  16. }
  17. }
  18. }
  19. }
  20. }
  21.  
  22. private static int getHammingDistance( final String s1, final String s2 ) {
  23. assert ( s1.length() == s2.length() );
  24. int dist = 0;
  25. for ( int i = 0; i < s1.length(); i++ ) {
  26. if ( s1.charAt( i ) != s2.charAt( i ) ) ++dist;
  27. }
  28. return dist;
  29. }
  30.  
  31. }
  32.  
Success #stdin #stdout 0.1s 380160KB
stdin
Standard input is empty
stdout
a (0), a (0) - 0
a (0), a (1) - 0
a (0), b (2) - 1
a (0), b (3) - 1
a (0), a (4) - 0
b (1), a (0) - 1
b (1), a (1) - 1
b (1), b (2) - 0
b (1), b (3) - 0
b (1), a (4) - 1
b (2), a (0) - 1
b (2), a (1) - 1
b (2), b (2) - 0
b (2), b (3) - 0
b (2), a (4) - 1
a (3), a (0) - 0
a (3), a (1) - 0
a (3), b (2) - 1
a (3), b (3) - 1
a (3), a (4) - 0
b (4), a (0) - 1
b (4), a (1) - 1
b (4), b (2) - 0
b (4), b (3) - 0
b (4), a (4) - 1
ab (0), aa (0) - 1
ab (0), ab (1) - 0
ab (0), bb (2) - 1
ab (0), ba (3) - 2
bb (1), aa (0) - 2
bb (1), ab (1) - 1
bb (1), bb (2) - 0
bb (1), ba (3) - 1
ba (2), aa (0) - 1
ba (2), ab (1) - 2
ba (2), bb (2) - 1
ba (2), ba (3) - 0
ab (3), aa (0) - 1
ab (3), ab (1) - 0
ab (3), bb (2) - 1
ab (3), ba (3) - 2
abb (0), aab (0) - 1
abb (0), abb (1) - 0
abb (0), bba (2) - 2
bba (1), abb (1) - 2
bba (1), bba (2) - 0
bab (2), aab (0) - 1
bab (2), abb (1) - 2
bab (2), bba (2) - 2
abba (0), aabb (0) - 2
abba (0), abba (1) - 0