Answer to Question #266420 in Java | JSP | JSF for sweta

Question #266420

Hallindrome


Given a string S. Let us divide 5 into two equal parts s1 and s2.s is called a halindrome if at least any one of the following conditions satisfy:


1.Sis a palindrome and length of S>=2


2.S1 is a halindrome.


3S2 a halindrome.


In the case of an odd length string the middle element is not present in both S1 and S2. If index of middle element is m. then, S1= S [0,m-1] and S2=S[m+1|S|-1].


Input Specification


input 1: Number of strings 1<=input1<=100.

imput2:An array of size input1 containing strings 2<=length of each string<=100.

output specification:

for each test case, return the number of strings which are halindromes.

EX:input1:1

input2:{harshk}

output 0

explanation:1.string S is not forming a palindrome.

2.string S1=har,which is not a halindrome.

3.String S2=shk,which is not a halindrome.

as none of the conditions are true,hence output to be returned is 0.


please give java coding solution for this question



1
Expert's answer
2021-11-15T17:28:11-0500
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main
{
    public static void main(String args[])
    {
        String string1,string2,StringS,X,Y,Rever;
        int M1,M2;
        Scanner input=new Scanner(System.in);
        int[] array=new int[30];
        string1=input.next();
        string2=input.next();
        M1=string1.length();
        M2=string2.length();
        int count=0,minimum=0;
        for(int i=0;i<=M1-M2;i++)
        {
            if(i==0)
            {
                X=string2;
                Y=string1.substring(i+M2,M1);
                StringS=X+Y;
            }
            else
            {
                X=string1.substring(0,i);
                Y=string2;
                StringS=X+Y+string1.substring(i+M2,M1);
            }
            StringBuilder B=new StringBuilder(StringS);
            B.reverse();
            Rever=B.toString();
            if(StringS.equals(Rever))
            {
                for(int j=0;j<string2.length();j++)
                {
                    if(string2.charAt(j)!=string1.charAt(i+j))
                    {
                        count+=1;
                    }    
                }
                if(count<minimum)
                {
                    minimum=count;
                }
            }
        }
        System.out.println(minimum);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS