🧰 UtlKit

How to Use the String Similarity Tool: Levenshtein Distance and a Percent Score from Two Strings, in the Browser

Two text strings, compared with the Levenshtein edit distance, a percent similarity from the longer length minus the distance, and the full edit script, all computed in the browser.

The String Similarity tool takes two text strings, String A and String B, which default to kitten and sitting, and computes the Levenshtein edit distance between them in the browser. On every keystroke the result recomputes live: three stat cards print the distance, the similarity percentage and the operation count, like 3, 57.1% and 7 for the defaults, and below them a progress bar and a numbered, color-coded edit script list each step from the first character to the last. Everything is a browser-side dynamic-programming pass over the two strings: nothing is sent anywhere, and no upload happens at any point.

What the tool takes and what it gives

The input is two single-line text fields side by side, labeled String A and String B, in a monospace font, and nothing else is asked. The defaults are kitten in A and sitting in B, and the same two words appear as the field placeholders. The output is three cards in a row: the distance as a plain integer, the similarity as a percentage with one decimal place, and the operation count as a plain integer. Under the cards, a progress bar fills to the same percentage, and a scrollable list prints the full edit script, each line numbered from 1, each colored by its type: replace in yellow, insert in blue, delete in red, and match in the muted text color. There is no button: the result recomputes as you type, because the whole computation is a memoized read of the two field values.

How the distance is computed

The distance is the classic Levenshtein distance, built with a dynamic-programming table of (length of A + 1) by (length of B + 1) cells. Each cell takes the minimum of three neighbors plus a cost: the cell above plus one, which is a delete; the cell to the left plus one, which is an insert; and the diagonal plus a cost of zero when the two characters are equal and one when they are not, which is a match or a replace. The comparison is character by character and case-sensitive, so K and k are different characters. For the defaults, kitten against sitting, the table is 7 by 8, 56 cells, and the bottom-right cell reads 3: replace the k with an s, replace the e with an i, and insert the g. book against back is 2, the two o positions; flaw against lawn is 2, the delete of the f and the insert of the n at the end.

What the operation list shows

The list is a back-trace through the finished table, walking from the bottom-right corner back to the top-left. At each step the trace prefers a match when the two current characters are equal, then a replace, then an insert, then a delete, and the collected steps are printed in reverse, first to last. That walk yields one valid minimum edit script, not the only one: different equal-cost choices at a tie would give a different list of the same length. Matches are listed too, so the operation count is the length of the script, not the distance. kitten against sitting prints 7 operations for a distance of 3, the four matches being i, t, t and n; kitten against itself prints 6 operations, all matches, for a distance of 0; flaw against lawn prints 5, the two edits plus three matches.

How the similarity percentage is read out

The percentage is the longer length minus the distance, divided by the longer length, times one hundred, printed with one decimal. For the defaults, 7 minus 3 over 7, it reads 57.1%, which sits in the yellow band: the number and the bar turn green at 70 or above, yellow at 40 or above, and red below 40. Identical strings read 100.0% at distance 0. A single case flip, Kitten against kitten, is a distance of 1 over a length of 6, reading 83.3%. An accent counts as a character difference: café against cafe is distance 1, reading 75.0%. And a surrogate pair counts as two units, so the smiley in a😀b against ab is a distance of 2, reading 50.0%.

When the result is blank

If either field is empty after trimming, the result is null and nothing below the two inputs renders: no cards, no bar, no list. Whitespace inside or at the edges is not trimmed from the comparison, only checked for emptiness, so a trailing space is one edit: key with a trailing space against key is a distance of 1, reading 75.0% over the length of 4. Against an empty string the distance is the whole length of the other side, which is why abc against the empty side computes to 3 and 0.0%, even though the page would not show it while the field stays empty.

How long the strings can be

There is no length cap in the code. The cost is the table size, (length of A + 1) times (length of B + 1) cells, so the work grows with the product of the two lengths: kitten against sitting is 56 cells, two 1,000-character strings are about one million cells, and two 10,000-character strings are about one hundred million, which the page will compute but slowly. The inputs are single-line fields, so very long strings are typed or pasted on one line.

What it is not

The tool compares, it does not transform the text: to strip the extra whitespace and lines before comparing, use the Whitespace Cleaner; to flip a string end to end, the Text Reverser does that; and to count characters without a second string, the Character Counter & Analyzer does. For the walkthrough of the reversing tool, see the text reverser guide.

Related Tools

Frequently Asked Questions

Does the String Similarity tool upload my strings to a server?

No. The two fields are read by the page, and the only computation is the Levenshtein table, built character by character in the browser, so the strings never leave the device and no network request is made at any point. There is no history and no stored comparison: closing or reloading the tab discards both fields and the result.

Why is the operation count higher than the distance?

The operation list is a back-trace through the finished table, and it lists the matches too. kitten against sitting has a distance of 3, but the script has 7 lines, the four matches being i, t, t and n. The distance counts only the edits, the replace, insert and delete lines; the operation count is the length of the whole script.

Is the comparison case-sensitive?

Yes. The table compares the characters with an equality test that distinguishes K from k, so Kitten against kitten is a distance of 1 over a length of 6, reading 83.3%, not a distance of 0. If you want a case-insensitive comparison, convert both fields to the same case before you paste them in.

Why does one accent change the score so much?

An accented letter is a different character from its unaccented form, so it costs one edit: café against cafe is a distance of 1, reading 75.0%. The same holds for a smiley, which the page counts as two units, so a😀b against ab is a distance of 2, reading 50.0%. The distance follows the characters exactly as they sit in the fields.

What does the color of the percentage mean?

The number and the progress bar share three bands: green at 70 or above, yellow at 40 or above, and red below 40. The defaults, 57.1% for kitten against sitting, sit in the yellow band. The color is a read of the same percentage, not a separate verdict.

Related Articles