Java Solutions : Minimum Operations to Convert Strings | Hacker Rank Solutions | StudyEcart


Problem: Minimum Operations to Convert Strings

Given two strings `word1` and `word2`, your task is to determine the minimum number of operations required to convert `word1` to `word2`. You are allowed to perform the following three operations:

1. Insert a character.

2. Delete a character.

3. Replace a character.


Write a function that takes in `word1` and `word2` as inputs and returns the minimum number of operations required.


Example 1:

Input: word1 = "horse", word2 = "ros"

Output: 3

Explanation: 

   horse -> rorse (replace 'h' with 'r')

   rorse -> rose (remove 'r')

   rose -> ros (remove 'e')


Example 2:

```

Input: word1 = "intention", word2 = "execution"

Output: 5

Explanation: 

   intention -> inention (remove 't')

   inention -> enention (replace 'i' with 'e')

   enention -> exention (replace 'n' with 'x')

   exention -> exection (replace 'n' with 'c')

   exection -> execution (insert 'u')


Example 3:

Input: word1 = "kitten", word2 = "sitting"

Output: 3

Explanation:

   kitten -> sitten (replace 'k' with 's')

   sitten -> sittin (replace 'e' with 'i')

   sittin -> sitting (insert 'g')


Example 4:

Input: word1 = "abc", word2 = "def"

Output: 3

Explanation:

   abc -> dbc (replace 'a' with 'd')

   dbc -> dbc (remove 'd')

   dbc -> def (replace 'c' with 'f')


Java Code :

 
  
import java.util.Scanner;

public class MinOperationsToConvertStrings {

    public static int minOperations(String word1, String word2) {
        int m = word1.length();
        int n = word2.length();

        int[][] dp = new int[m + 1][n + 1];

        for (int i = 0; i <= m; i++) {
            for (int j = 0; j <= n; j++) {
                if (i == 0) {
                    dp[i][j] = j;
                } else if (j == 0) {
                    dp[i][j] = i;
                } else if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = 1 + Math.min(dp[i - 1][j - 1], Math.min(dp[i][j - 1], dp[i - 1][j]));
                }
            }
        }

        return dp[m][n];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first word: ");
        String word1 = scanner.nextLine();

        System.out.print("Enter the second word: ");
        String word2 = scanner.nextLine();

        scanner.close();

        int operations = minOperations(word1, word2);
        System.out.println("Minimum operations required: " + operations);
    }
}

(code-box)

Boost your Java programming skills with these top 10 beginner-friendly Java programs! Check them out Clickhere.

🚀 Elevate Your Career with Studyecart! 📚📊

🔥 Get instant job notifications and ace interviews with Studyecart. 📌

Download

#StudyecartApp #CareerBoost

Post a Comment

0 Comments

Top Post Ad

Bottom Post Ad