Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code to find the palindromes in an array of strings using Brute Force method with O(n2) time complexity:

 for(int i=0;i<s.length;i++) {

            for(int j=i+1;j<s.length;j++) {

                if(s[i] == reverse(s[j])) {

                    count++;

                }

            }

        }

Can anyone tell me how to implement the above without using Hashing and with O(n) time complexity?

1 Answer

0 votes
by (19.7k points)

You can check the String as Palindrome like below: 

String st1 = "RACECAR";

int flag=0;   

    for (int j=0;j<=str1.length()-1 ;j++)   

    {   

     if (st1.charAt(j)!=st1.charAt(st1.length()-j-1))   

     {   

     flag=1;   

     break;   

     }   

    }  

  if(flag==1)   

  System.out.println("String is not a palindrome");   

  else   

    System.out.println("String is a palindrome"); 

Here is the Output :- String is a palindrome

Interested in Java? Check out this Java Certification by Intellipaat.  

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 18, 2021 in Java by Jake (7k points)
0 votes
1 answer
asked Feb 15, 2021 in Java by Jake (7k points)
+1 vote
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...