1. Scatter-Palindrome
A palindrome is a string which reads the same forwa
and backwards, for example, tacocat and mom. A
string is a scatter palindrome if its letters can be
rearranged to form a palindrome. Given a string,
determine how many of its substrings are scatter-
palindromes. A substring is a contiguous range of
characters within the string.
For example, given a string aabb, the scatter-
palindromes are a, aa, aab, aabb, a, abb, b, bb, b.
There are 9 substrings that are scatter-palindromes.
Write a program that takes input in the below given
format and prints output in the below given format.
Constraints
• 1 s size of string s 1000
• all characters of string e ascii[a-z]
Input Format For Custom Testing
Sample Case o
Sample Case 1
Answers
Answer:
import java.util.Scanner;
class palindrome
{
static String reverse(String s)
{
char[] letters = new char[s.length()];
int ind = 0;
for(int i = s.length() - 1; i >= 0; i--)
{
letters[ind] = s.charAt(i);
ind++;
}
String reversed = "";
for(int i = 0; i < s.length(); i++)
{
reversed = reversed + letters[i];
}
return reversed;
}
static boolean isPalindrome(String str)
{
if(reverse(str).equals(str))
{
return true;
}
else
{
return false;
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string:");
String x = sc.nextLine();
if(isPalindrome(x))
{
System.out.println(x + " is a palindrome");
}
else
{
System.out.println(x + " is not a palindrome");
}
}
}
Explanation: