Write a java program to check palindrome string at least two different vowels

Write a program to check if a given string is a palindrome and contains at least two different vowels.

Note: Case Insensitive while considering vowel, i.e a & A are the same vowels, But Case sensitive while considering palindrome i.e abc CbA are not palindromes.

Input and Output Format

  • Input consists of a string with a maximum size of 100 characters.
  • Output consists of a single Integer.

Refer sample output for formatting specifications

Sample Input 1:

abceecba

Sample Output 1:

1

Sample Input 2:

abcd

Sample Output 2:

-1

Java program to check palindrome string at least two different vowels

Following are the steps to check palindrome string that contains at least two different vowels:

  • Input a string from the user.
  • Call display() method with the input string.
  • Inside the method, initialized the string buffer with the specified string.
  • Declare and initailaize variable k with 0.
  • Create a LinkedHashSet of characters. (to store unique element)
  • Now, reverse the string in the string buffer.
  • Compare the input string with the string in the string buffer and if it is equal, then
    • Convert it to lower case.
    • retrieve each character and add it to LinkedHashSet.
  • Now, iterate over the LinkedHashSet and perform the following steps
    • Fetch each character and check if that character is a vowel. If it is a vowel increment k value.
    • Now, if the value of k is greater than 2, then return 1.
    • Otherwise, return -1.
  • End of the program.
package testCases;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;
public class MainJava {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(display(s));
}
 
public static int display(String s) {
StringBuffer sb = new StringBuffer(s);
int k = 0;
LinkedHashSet<Character> l1 = new LinkedHashSet<Character>();
String s2 = sb.reverse().toString();
if (s2.equals(s)) {
String s3 = s2.toLowerCase();
 
for (int i = 0; i < s3.length(); i++) {
l1.add(s3.charAt(i));
}
Iterator<Character> it = l1.iterator();
while (it.hasNext()) {
char a = it.next();
 
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
k++;
}
}
if (k >= 2)
return 1;
else
return -1;
 
}
 
}

Output

Convert String into a palindrome string in Java

Obtain a string from the user and check if the string is able to convert into a palindrome by changing only one character. If it is possible then print “No Changes required” else print “Changes required”.

Input and Output Format

  • Input consists of a string with a maximum size of 100 characters.
  • Output consists of a string

Refer sample output for formatting specifications

Sample Input 1:

abc

Sample Output 1:

No Changed required

Sample Input 2:

abccca

Sample Output 2:

Changed is required

Program to convert into a palindrome by changing only one character

  • Input a string from the user.
  • Call the display() method with the input string.
  • Inside the method, change the string to lowercase.
  • Now, iterate over it and check character at i and ((s.length()/2)-i-1).
  • If they are not the same, then increment the count.
  • If the count is greater than 1 then print “Change is required” otherwise “No change required”.
package com.testcases;

import java.util.Scanner;

public class Program {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        display(str);
    }

    private static void display(String str) {
        String s = str.toLowerCase();
        int count = 0;
        for(int i = 0;i < s.length()/2;i++){
            if(s.charAt(i)!=s.charAt((s.length()/2)-i-1))
                count++;
        }
        if(count>=1)
            System.out.println("Changed is required");
        else
            System.out.println("No changed required");

    }
}

Output

Thus, in this way, we wrote a java program to check palindrome string at least two different vowels.