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

3.1 Exp

The document contains details of a student named Harshit with UID 20BCS9276 studying in section MM 701 A. It includes code for a function to find the longest palindrome substring in a given string. The code uses nested for loops to compare characters at index i and j to find the longest palindrome, which it then prints along with the length.

Uploaded by

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

3.1 Exp

The document contains details of a student named Harshit with UID 20BCS9276 studying in section MM 701 A. It includes code for a function to find the longest palindrome substring in a given string. The code uses nested for loops to compare characters at index i and j to find the longest palindrome, which it then prints along with the length.

Uploaded by

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

NAME – Harshit

UID – 20BCS9276
SECTION – MM 701 A
SUBJECT – PROJECT BASED LEARNING IN JAVA

WORKSHEET 3.1

Q1.

ANS1.

import java.util.*;

class GFG{

// Function to print a subString str[low..high] static


void printSubStr(String str, int low, int high)
{
for (int i = low; i <= high; ++i)
System.out.print(str.charAt(i));
}
// This function prints the
// longest palindrome subString
// It also returns the length // of the
longest palindrome static int
longestPalSubstr(String str)
{
// get length of input String
int n = str.length();

// All subStrings of length 1


// are palindromes int
maxLength = 1, start = 0;

// Nested loop to mark start and end index


for (int i = 0; i < str.length(); i++) {
for (int j = i; j < str.length(); j++) {
int flag = 1;

// Check palindrome
for (int k = 0; k < (j - i + 1) / 2; k++)
if (str.charAt(i + k) != str.charAt(j - k))
flag = 0;

// Palindrome if (flag!=0 && (j


- i + 1) > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}

System.out.print("Longest palindrome subString is: ");


printSubStr(str, start, start + maxLength - 1);

// return length of LPS


return maxLength;
}

// Driver Code public static void


main(String[] args)
{
String str = "forgeeksskeegfor";
System.out.print("\nLength is: "
+ longestPalSubstr(str));
}
}

OUTPUT –

You might also like