Skip to content
DeepForgeby svx
All problems
HardNLPnlp-033

Jaro Similarity

Problem

Return the Jaro similarity between two strings, which is always in [0, 1]. Two characters match when they are equal and at most floor(max(len(s1), len(s2)) / 2) - 1 positions apart.

Count transpositions as half the number of matched characters that appear out of order. Return 1.0 for equal strings and 0.0 when there are no matches.

Hint

Match characters inside a sliding window first, then count order differences.

Test cases

case 1
in: ["martha","marhta"]
expected: 0.9444444444444445
case 2
in: ["dwayne","duane"]
expected: 0.8222222222222223
case 3
in: ["abc","abc"]
expected: 1
case 4
in: ["abc","xyz"]
expected: 0
case 5
in: ["","a"]
expected: 0
Starter codePython
def jaro(s1, s2):
    # Your code here
    pass

5 test cases · run your code against them in the editor