One letter mismatch

I want to find out that the string has one mismatch.

1)CHARACTER INTERCHANGE W/O DISTURBING PATTERN ACRPG0182F v/s ACRPG0812F ACNPA4428K v/s ACHPA4428K 2)CHARACTER LENGTH DIFFERS BY 1 DIGIT ACRPG0182F v/s ACRPG0812 ACRPG0182F v/s CRPG0812F 

In case 1, both line lengths are the same, but have 1 character mismatch
In case 2, both line lengths differ by 1, and any character may be incompatible in both lines.

+4
source share
4 answers
  • get number using regex ( \d+ )
  • fill a Set all characters ( numberStr.toCharArray() )
  • use guava Sets - Sets.difference(set1, set2) and see if it has exactly one element

Initially, I thought that you need to check the difference differently: only one digit is different, and not "the string contains only one digit, regardless of order." If order is also important, just calculate the levenshtein distance and see if it is 1.

StringUtils.getLevenshteinDistance(s1, s2) from commons-lang will do this.

+4
source

As Bojo mentioned, using levenstein distance is probably the most direct way to solve your problem. The definition from this page is exactly what you are asking for.

Levenshtein distance (LD) is a measure of the similarity between two lines, which we will call the source line (s) and the target line (t). Distance is the number of deletions, insertions, or replacements needed to convert s to t.

+2
source

Define an Xor function for strings.

0
source

It sounds like Levenshtein distance 1 , look at the algorithm in the link.

0
source

Source: https://habr.com/ru/post/1397033/


All Articles