Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# hammingDistanceCodePoints

> Calculate the [Hamming distance][hamming-distance] between two equal-length strings by comparing Unicode code points.

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var hammingDistanceCodePoints = require( '@stdlib/string/base/distances/hamming-code-points' );
```

#### hammingDistanceCodePoints( s1, s2 )

Calculates the [Hamming distance][hamming-distance] between two equal-length strings by comparing Unicode code points.

```javascript
var dist = hammingDistanceCodePoints( 'frog', 'from' );
// returns 1

dist = hammingDistanceCodePoints( 'tooth', 'froth' );
// returns 2

dist = hammingDistanceCodePoints( 'cat', 'cot' );
// returns 1

dist = hammingDistanceCodePoints( '', '' );
// returns 0

// Emoji are treated as single Unicode code points:
dist = hammingDistanceCodePoints( '👋', '🌍' );
// returns 1

dist = hammingDistanceCodePoints( 'a👋b', 'c🌍d' );
// returns 3
```

</section>

<!-- /.usage -->

<!-- Package notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If the two strings differ in the number of Unicode code points, the [Hamming distance][hamming-distance] is not defined. Consequently, when provided two input strings with an unequal number of Unicode code points, the function returns a sentinel value of `-1`.
- Unlike the UTF-16 code unit implementation in `@stdlib/string/base/distances/hamming`, this function iterates over **Unicode code points** rather than UTF-16 code units. This means surrogate pairs (used to encode characters outside the Basic Multilingual Plane, such as most emoji) are treated as a single unit of comparison. For example, the emoji `'👋'` (U+1F44B) is encoded as a UTF-16 surrogate pair `\uD83D\uDC4B` and has a `String.length` of `2`, but this function treats it as a single code point.
- The function is **not** grapheme-cluster aware. Characters composed of multiple Unicode code points (e.g., family emoji built from multiple code points joined by Zero Width Joiners, or letters with combining diacritical marks) are treated as multiple code points.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

```javascript
var hammingDistanceCodePoints = require( '@stdlib/string/base/distances/hamming-code-points' );

var dist = hammingDistanceCodePoints( 'algorithms', 'altruistic' );
// returns 7

dist = hammingDistanceCodePoints( 'elephant', 'hippopod' );
// returns 7

dist = hammingDistanceCodePoints( 'javascript', 'typescript' );
// returns 4

dist = hammingDistanceCodePoints( 'hamming', 'ladybug' );
// returns 5

// Emoji strings (each emoji = 1 Unicode code point):
dist = hammingDistanceCodePoints( '👋🌍🎉', '🌟💫✨' );
// returns 3

// Mixed ASCII and emoji:
dist = hammingDistanceCodePoints( 'hello👋', 'hallo🌍' );
// returns 2
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[hamming-distance]: https://en.wikipedia.org/wiki/Hamming_distance

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var pkg = require( './../package.json' ).name;
var hammingDistanceCodePoints = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var value;
var out;
var i;

values = [
[ 'algorithms', 'altruistic' ],
[ '1638452297', '4444884447' ],
[ '', '' ],
[ 'z', 'a' ],
[ 'aaappppk', 'aardvark' ],
[ 'frog', 'flog' ],
[ 'fly', 'ant' ],
[ 'elephant', 'hippopod' ],
[ 'hippopod', 'elephant' ],
[ 'hippo', 'zzzzz' ],
[ 'hello', 'hallo' ],
[ '👋🌍🎉', '🌟💫✨' ],
[ 'a👋b', 'c🌍d' ],
[ 'congratulations', 'conmgeautlatins' ]
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
value = values[ i%values.length ];
out = hammingDistanceCodePoints( value[0], value[1] );
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
}
b.toc();
if ( typeof out !== 'number' ) {
b.fail( 'should return a number' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

{{alias}}( s1, s2 )
Calculates the Hamming distance between two equal-length strings by
comparing Unicode code points.

The function returns a sentinel value of -1 if the two input strings differ
in the number of Unicode code points.

Parameters
----------
s1: string
First input string.

s2: string
Second input string.

Returns
-------
out: number
Hamming distance.

Examples
--------
> var d = {{alias}}( 'algorithms', 'altruistic' )
7
> d = {{alias}}( 'elephant', 'hippopod' )
7
> d = {{alias}}( 'javascript', 'typescript' )
4
> d = {{alias}}( '👋', '🌍' )
1
> d = {{alias}}( 'a👋', 'b🌍' )
2

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Calculates the Hamming distance between two equal-length strings by comparing Unicode code points.
*
* ## Notes
*
* - The function returns a sentinel value of `-1` if the two input strings differ in the number of Unicode code points.
*
* @param str1 - first input string
* @param str2 - second input string
* @returns Hamming distance
*
* @example
* var dist = hammingDistanceCodePoints( 'fly', 'ant' );
* // returns 3
*
* @example
* var dist = hammingDistanceCodePoints( '👋', '🌍' );
* // returns 1
*
* @example
* var dist = hammingDistanceCodePoints( 'algorithms', 'altruistic' );
* // returns 7
*/
declare function hammingDistanceCodePoints( str1: string, str2: string ): number;


// EXPORTS //

export = hammingDistanceCodePoints;
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import hammingDistanceCodePoints = require( './index' );


// TESTS //

// The function returns a number...
{
hammingDistanceCodePoints( '', '' ); // $ExpectType number
hammingDistanceCodePoints( 'fly', 'ant' ); // $ExpectType number
hammingDistanceCodePoints( '👋', '🌍' ); // $ExpectType number
}

// The compiler throws an error if the function is provided a first argument which is not a string...
{
hammingDistanceCodePoints( true, '' ); // $ExpectError
hammingDistanceCodePoints( false, '' ); // $ExpectError
hammingDistanceCodePoints( null, '' ); // $ExpectError
hammingDistanceCodePoints( undefined, '' ); // $ExpectError
hammingDistanceCodePoints( 5, '' ); // $ExpectError
hammingDistanceCodePoints( [], '' ); // $ExpectError
hammingDistanceCodePoints( {}, '' ); // $ExpectError
hammingDistanceCodePoints( ( x: number ): number => x, '' ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a string...
{
hammingDistanceCodePoints( '', true ); // $ExpectError
hammingDistanceCodePoints( '', false ); // $ExpectError
hammingDistanceCodePoints( '', null ); // $ExpectError
hammingDistanceCodePoints( '', undefined ); // $ExpectError
hammingDistanceCodePoints( '', 5 ); // $ExpectError
hammingDistanceCodePoints( '', [] ); // $ExpectError
hammingDistanceCodePoints( '', {} ); // $ExpectError
hammingDistanceCodePoints( '', ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
hammingDistanceCodePoints(); // $ExpectError
hammingDistanceCodePoints( '' ); // $ExpectError
hammingDistanceCodePoints( '', '', 3 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var hammingDistanceCodePoints = require( './../lib' );

console.log( hammingDistanceCodePoints( 'algorithms', 'altruistic' ) );
// => 7

console.log( hammingDistanceCodePoints( 'elephant', 'hippopod' ) );
// => 7

console.log( hammingDistanceCodePoints( 'javascript', 'typescript' ) );
// => 4

// All emoji strings:
console.log( hammingDistanceCodePoints( '👋🌍🎉', '🌟💫✨' ) );
// => 3

// Mixed ASCII and emoji strings:
console.log( hammingDistanceCodePoints( 'a👋b', 'c🌍d' ) );
// => 3

// Unequal code-point lengths return -1:
console.log( hammingDistanceCodePoints( 'a', 'abcissa' ) );
// => -1
Loading