Question 1

Question 1: Check Palindrome
Statement:Chef is given a string . If he can convert a string into a palindrome by rearranging the characters, he will keep that string, otherwise he will discard that string. You’ve to tell if Chef will keep that string or not.
Note : A palindrome is a word which reads the same backward or forward.
Input Format: The first line of input contains the number of test cases T. Each test case contains a single string S.
Output Format: For each test case print Yes if Chef can convert string into a palindrome by rearranging the characters else print No.
Constraints:
1<=T<=10
1<=∣S∣<=100
Sample Input:
3
king
noon
abb
Sample output:
No
Yes
No
Solution:
C++
Python
Java
C++
Run
#include<bits/stdc++.h> using namespace std; bool canBePalindrome(string s) { unordered_mapfreq; // Count the frequency of each character in the string for (char c : s) { freq[c]++; } int oddCount = 0; // Count how many characters have an odd frequency for (auto it : freq) { if (it.second % 2 != 0) { oddCount++; } } // For a string to be a palindrome, it can have at most one character with an odd frequency return oddCount <= 1; } int main() { int T; cin >> T; while (T--) { string s; cin >> s; if (canBePalindrome(s)) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
Python
def can_be_palindrome(s): freq = {} for char in s: freq[char] = freq.get(char, 0) + 1 odd_count = 0 for count in freq.values(): if count % 2 != 0: odd_count += 1 return odd_count <= 1 T = int(input()) for _ in range(T): s = input() if can_be_palindrome(s): print("Yes") else: print("No")
Java
Run
import java.util.*; class Main { public static boolean canBePalindrome(String s) { Mapfreq = new HashMap<>(); // Count the frequency of each character in the string for (char c : s.toCharArray()) { freq.put(c, freq.getOrDefault(c, 0) + 1); } int oddCount = 0; // Count how many characters have an odd frequency for (int count : freq.values()) { if (count % 2 != 0) { oddCount++; } } // For a string to be a palindrome, it can have at most one character with an odd frequency return oddCount <= 1; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); sc.nextLine(); // Consume the newline character while (T-- > 0) { String s = sc.nextLine(); if (canBePalindrome(s)) { System.out.println("Yes"); } else { System.out.println("No"); } } } }