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

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 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...
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
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...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT