Question

Write an algorithm that takes two strings X and Y and a positive integer k as...

Write an algorithm that takes two strings X and Y and a positive integer k as input, and determines whether Y can be turned into X by at most k insertions or deletions. For example: let X = “abacad", Y = “cebacad" and k = 3 then your algorithm should give a yes answer because Y can be turned into X in 3 steps by a left-to-right processing as follows: delete leftmost c delete “e" insert “a" Give an tight asymptotic bound on the running time of your algorithm. JAVA CODE

VERY URGENT THANKS

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;

import java.util.Scanner;

  

class LeastCommonSubsequence {

      

    // Returns length of length common  

    // subsequence for a[0..m-1],  

    // b[0..n-1]

    static int lcs(String X, String Y)

    {

        int m = X.length();

        int n = Y.length();

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

        int i, j;

  

        // Following steps build MAT[m+1][n+1] in

        // bottom up fashion. Note that MAT[i][j]

        // contains length of LCS of a[0..i-1]

        // and b[0..j-1]  

        for (i = 0; i <= m; i++)

        {

            for (j = 0; j <= n; j++)

            {

                if (i == 0 || j == 0)

                    MAT[i][j] = 0;

                else if (X.charAt(i-1) == Y.charAt(j-1))

                    MAT[i][j] = MAT[i-1][j-1] + 1;

                else

                    MAT[i][j] = Math.max(MAT[i-1][j], MAT[i][j-1]);

            }

        }

        // MAT[m][n] contains length of LCS  

        // for X[0..n-1] and Y[0..m-1]  

            return m+n- 2*MAT[m][n]; //least number of insertion or deletion

        }   

    // Driver program to test above

    public static void main(String[] args)

    {

        String X, Y;

        int k;

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter 1st string X: ");        

        X = sc.next();

        System.out.print("Enter 2nd string Y: ");        

        Y = sc.next();

        System.out.print("Enter value of k: ");        

        k = sc.nextInt();

        int l = lcs(X,Y);

        if(k<l){

            System.out.println("No");

        }

        else

        {

            System.out.println("Yes");     

        }

    }  

}

Add a comment
Know the answer?
Add Answer to:
Write an algorithm that takes two strings X and Y and a positive integer k as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following variant of string alignment: given two strings x,y, and a positive integer L,...

    Consider the following variant of string alignment: given two strings x,y, and a positive integer L, find all contiguous substrings of length at least L that are aligned (using no-ops = substituting a letter for itself) in some optimal alignment of x and y. Assume the costs of substitution, insertion, and deletion are given by constants csubs,cins,cdel, and that the cost of substituting a letter for itself is zero. (a) Show that the output here contains at most O((n−L)^3) substrings....

  • JAVA: Write a simple program that takes as input 2 16-bit strings, X and Y. Compute...

    JAVA: Write a simple program that takes as input 2 16-bit strings, X and Y. Compute and and display the bitwise AND, bitwise OR, and bitwise XOR of the two strings.

  • Discrete Mathematical Structures Draw a Turing machine that takes a string representing two unary numbers, x...

    Discrete Mathematical Structures Draw a Turing machine that takes a string representing two unary numbers, x and y, separated by a 0, and determines whether x greaterthanorequalto y. For example, the input for x = 3, y = 4 would be 11101111. Use two halt states: one for yes and one for no. Give the trace of your machine in the previous problem processing the strings 11101111 and 11110111. Draw a TM that computes f(w) = w^R where w elementof...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

    Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT