Java for Leetcode 072 Edit Distance

Source: Internet
Author: User

Given words word1 and word2, find the minimum number of steps required to convert word1 to Word2. (Each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

A) Insert a character
b) Delete a character
c) Replace a character

Problem Solving Ideas:

DP problem, Java implementation is as follows:

    public int Mindistance (string word1, String word2) {int[] dp = new Int[word2.length () + 1];for (int i = 0; i < Dp.lengt H i++) Dp[i] = i;for (int i = 1; I <= word1.length (); i++) {int upleft = dp[0];DP [0] = i;for (int j = 1; J <= Word2.len Gth (); J + +) {int temp = dp[j];if (Word1.charat (i-1) = = Word2.charat (j-1)) dp[j] = upleft;elsedp[j] = math.min (Upleft, Math.min (Dp[j], dp[j-1])) + 1;upleft = temp;}} Return Dp[word2.length ()];    }

Java for Leetcode 072 Edit Distance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.