Question

In: Computer Science

public int static mirroringIt(String str){ return n; } implement this function so its takes ONE string...

public int static mirroringIt(String str){

return n;

}

implement this function so its takes ONE string and remove the characters so it becomes a mirrored word.

example:

Input: qswwbi

Output: 4

remove q s b i

"wow" is a mirror word.

output : 0

Input : "if"

output : 1

remove either I or f because it's two words.

don't use 3rd parties library or java.util.

Solutions

Expert Solution

The java method for the problem is provided below, please comment if any doubts:

Note: The whole Java code is provided, the required function in the code is highlighted

public class Mirror {

    public static int mirroringIt(String str)

    {

                        //find the length of the input string

        int len = str.length();

        //to store the combination of strings

        int List[][] = new int[len][len];

        //store the single letters, its always mirrors

        for (int itr = 0; itr < len; itr++)

            List[itr][itr] = 1;

        //find the longest substring of the string

                        //that is mirror to each other

        for (int sub = 2; sub <= len; sub++)

        {

            for (int itr = 0; itr < len - sub + 1; itr++)

            {

                int k = itr + sub - 1;

                if (str.charAt(itr) ==

                        str.charAt(k) && sub == 2)

                    List[itr][k] = 2;

                else if (str.charAt(itr) ==

                              str.charAt(k))

                    List[itr][k] = List[itr + 1][k - 1] + 2;

                else

                    List[itr][k] = Integer.max(List[itr][k - 1],

                                         List[itr + 1][k]);

            }

        }

        //return the number of letters to be removed

        return (len-List[0][len - 1]);

    }

  

   //The main function to test the function

    public static void main(String[] args)

    {

        String str = "qswwbi";

        System.out.println(str +": "+mirroringIt(str));

        str= "if";

        System.out.println(str +": "+mirroringIt(str));

        str= "wow";

        System.out.println(str +": "+mirroringIt(str));

    }

}

Output:


Related Solutions

USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str)...
USE JAVA PLEASE Use recursion to implement a method public static int indexOf(String text, String str) that returns the starting position of the first substring of the text that matches str. Return –1 if str is not a substring of the text. For example, s.indexOf("Mississippi", "sip") returns 6. Hint: You must keep track of how far the match is from the beginning of the text. Make that value a parameter variable of a helper method.
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';        }        String temp="";        for (int i = 0; i < str.length(); i++) {                 if(!temp.contains(String.valueOf(str.charAt(i)))) {                     temp += String.valueOf(str.charAt(i));                 }             }        char[] tempArray=stringToArray(temp);        int[] countArr=new int[tempArray.length];        int max=0;        for(int i=0;i<tempArray.length;i++) {            int cnt=numOccurences(tempArray[i],str);            countArr[i]=cnt;...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
public static int example3 (int [] arr ) { int n = arr.length , total =...
public static int example3 (int [] arr ) { int n = arr.length , total = 0; for (int j = 0; j < n ; j += 2) for (int k = 0; k <= j ; k ++) total += arr [ j ]; return total ; } what is the running time of this method?
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;        for (int i = 2; i < n; i++){          if (n % i == 0)             return false; }        return true; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
public static void main(String [] args)     {         int[] a = new int[20];         a[0]...
public static void main(String [] args)     {         int[] a = new int[20];         a[0] = 0;         a[1] = 1;         for(int i = 2; i < 20; i++){             a[i] = a[i - 1] + a[i - 2];         }         for(int i = 0; i < a.length; i++)             System.out.println(a[i]);     } what would be the code when you convert this code, which is java into assembly code? Convert java into assembly code for the code...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
#include <iostream> using namespace std; string* callMe(string*& s){ static string* str = new string{"Chiqita Banana"}; str...
#include <iostream> using namespace std; string* callMe(string*& s){ static string* str = new string{"Chiqita Banana"}; str = s; return str; } int main() { string* s = new string{"Hey Google"}; string* str = callMe(s);    cout << "Call me \'" + *str +"\'"; return 0; } 1. What is the output or the error of this program? Call me 'Hey Google' 2. And please explain the data flow in detail (to memory level)? Please help question 2 with explanation, thank...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT