Home >Backend Development >PHP Tutorial >How to calculate distance between strings using php

How to calculate distance between strings using php

不言
不言Original
2018-07-10 11:28:281615browse

This article mainly introduces how to use PHP to calculate the distance between strings. It has a certain reference value. Now I share it with you. Friends in need can refer to it

1. Summary

Summary in one sentence: What is the best way to solve the dp problem: draw an example table after analyzing the status.

1. What is the best way to solve the dp problem?

After analyzing the status, example drawing a table

2. What are the benefits of drawing a picture?

It is easy to program after drawing the table, and it is not easy to make mistakes, because you have a reference, you can write the code according to the reference

2. Calculate the distance of strings

Title description

Levenshtein distance, also known as edit distance, refers to the minimum number of edit operations required to convert one string into the other between two strings. Permitted editing operations include replacing one character with another, inserting a character, and deleting a character. The algorithm of edit distance was first proposed by the Russian scientist Levenshtein, so it is also called Levenshtein Distance.

Ex:

String A: abcdefg

String B: abcdef

Achieve the purpose by adding or deleting the character "g" . Both options require one operation. Define the number of times required for this operation as the distance between two strings.

Requirements:

Given any two strings, write an algorithm to calculate their edit distance.

Please implement the following interface

/*  功能:计算两个字符串的距离
 *  输入: 字符串A和字符串B
 *  输出:无
 *  返回:如果成功计算出字符串的距离,否则返回-1
 */
     public   static   int calStringDistance (String charA, String  charB)
    {
        return  0;
    }

Input description:

Input two strings

Output description:

Get the calculation result

Example 1

Input

abcdefg
abcdef

Output

1

2. Code (the code is incorrect)

913909d5e92507b8b85e80e95374d4ec

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use PHP to obtain the analysis of images in documents

PHP simply implements sending emails and preventing them from being treated as spam Processing

How to modify the WordPress image address to a relative path

The above is the detailed content of How to calculate distance between strings using php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn