0% found this document useful (0 votes)
37 views3 pages

WORKSHEET-3.1 Java PDF New

3.1

Uploaded by

rohitjangra7944
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)
37 views3 pages

WORKSHEET-3.1 Java PDF New

3.1

Uploaded by

rohitjangra7944
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/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment- 3.1

Student Name: Lakshay Sindhu UID: 21BCS10420


Branch: CSE Section/Group: 21BCS CC 628 - B
Semester: 6th (Sixth) Date of Performance: 14/03/24
Subject Name: Project Based Learning in Java Lab Subject Code: 21CSH-319

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

2. Objective : Given a string, S, find its largest palindromic substring. Input Format: A string,
S. Constraints: 151000000. All the characters are lower-case English letters. Output Format:
Print the largest possible substring of S.

3. Procedure :
1. First, we need to consider the type of value.
2. If it is a number, we need to change it to a string to compare how it reads backwards and forwards.
3. If it is an object, we need to somehow also change it to a string to do a comparison.
4. If it is a string, we can forge ahead.
5. Compare a string with its reversed version.
6. Iterate using for loop and check to see if character on other side of string.
7. Use recursion to check the rst and last letters before invoking the function again with the shortened
string.

4. Code and Output :

Program Code :
import java.util.Scanner;

public class LongestPalindrome {


public String longestPalindrome(String s) {
if (s == null || s.length() < 1)
return “";
fi
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int start = 0, end = 0;


for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}

private int expandAroundCenter(String s, int left, int right) {


while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return right - left - 1;
}

public static void main(String[] args) {


LongestPalindrome p = new LongestPalindrome();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string:");
String input = scanner.nextLine();
System.out.println("The longest palindromic substring is: " + p.longestPalindrome(input));
}
}

Output :
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Learning Outcomes :

1. Learnt about Palindrome in java.

2. Learnt about recursion in java.

3. Learnt about dynamic programming.

4. Learnt about Longest Common Subsequence.

You might also like