0% found this document useful (0 votes)
13 views2 pages

PBLJ 8

The document describes an experiment to create a program that generates the longest palindrome possible from a given input string. It counts the frequency of each character, and uses those counts to construct the left and right halves of the palindrome.

Uploaded by

vikram9763s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

PBLJ 8

The document describes an experiment to create a program that generates the longest palindrome possible from a given input string. It counts the frequency of each character, and uses those counts to construct the left and right halves of the palindrome.

Uploaded by

vikram9763s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3.1
Student Name: Vikram Kumar Singh UID: 21BCS11831
Branch: BE-CSE Section/Group: CC-625-B
Semester: 6th Date of Performance: 27-03-2024
Subject Name: PBLJ Lab Subject Code: 21CSP-319

1. Aim:
Create a palindrome creator application for making a longest possible
palindrome out of given input string.

2. Objective:
• To learn about concept of HashMap in java.
• To learn about concept of string in java

3. Script and output:


package unit3;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class PalindromeCreator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the input string:");
String inputString = scanner.nextLine();
String palindrome = createPalindrome(inputString);
System.out.println("Longest Palindrome: " + palindrome);
scanner.close();
}

private static String createPalindrome(String input) {


// Count the frequency of each character
Map<Character, Integer> charFrequency = new HashMap<>();
for (char ch : input.toCharArray()) {
charFrequency.put(ch, charFrequency.getOrDefault(ch, 0) + 1);
}

StringBuilder leftHalf = new StringBuilder();


StringBuilder rightHalf = new StringBuilder();
char middleChar = '\0';
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

// Construct the left and right halves of the palindrome


for (Map.Entry<Character, Integer> entry : charFrequency.entrySet()) {
char ch = entry.getKey();
int frequency = entry.getValue();

// If frequency is even, add half occurrences to both left and right halves
if (frequency % 2 == 0) {
int halfFrequency = frequency / 2;
leftHalf.append(String.valueOf(ch).repeat(halfFrequency));
rightHalf.insert(0, String.valueOf(ch).repeat(halfFrequency));
} else {
// If frequency is odd, add one occurrence to the middle and the rest to both halves
middleChar = ch;
int halfFrequency = (frequency - 1) / 2;
leftHalf.append(String.valueOf(ch).repeat(halfFrequency));
rightHalf.insert(0, String.valueOf(ch).repeat(halfFrequency));
}
}

// Combine left half, middle character (if any), and right half
StringBuilder palindrome = new StringBuilder(leftHalf);
if (middleChar != '\0') {
palindrome.append(middleChar);
}
palindrome.append(rightHalf);

return palindrome.toString();
}
}

Output:

You might also like