Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Longest Palindrome String in java

class Solution {
   String getLongestPalindrome(String s) {
       int n = s.length();
       int index  = 0, palindromeLength = 1;
       for (int i = 1; i < n; i++) {
           int left = i - 1, right = i;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           left = i - 1;
           right = i + 1;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           
       }
       String ans = "";
       for (int i = index; i < index + palindromeLength; i++) {
           ans += s.charAt(i);
       }
       return ans;
   }
}
Comment

how to find palindrome numbers in java

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        int temp = num;

        int rev = 0;
        while (num > 0) {
            rev = rev * 10 + num % 10;
            num /= 10;
        }
        System.out.println(temp==rev?"palindrome":"not palindrome");
    }
Comment

Palindrome java

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please input a word: ");
    String line = keyboard.nextLine();

    String cleanLine = line.replaceAll("[W]", "");
    String reverse = new StringBuilder(cleanLine).reverse().toString();
    boolean isPalindrome = cleanLine.equals(reverse);

    System.out.print("It is " + isPalindrome + " that this word is a palindrome.");
}
Comment

String Palindrome in Java

import java.util.*;
import java.lang.*;
import java.io.*;


 class Main {

    static boolean isPalindrome(String s)
    {

       /* Two pointers initialised to 0 and 's.length()-1'.*/
        int i = 0, j = s.length() - 1;

        /* Looping through the string */
        while (i < j) {

            /* Even if one character is not equal, we will return false. */
            if (s.charAt(i) != s.charAt(j))
                return false;
               
            /* Value of pointers is changed */
            i++;
            j--;
        }
       
        /* This means all characters are same and thus string is a palindrome*/
        return true;
    }

    public static void main(String[] args)
    {
        String s = "abba";

        /* First, We'll convert it to lower case. */
        s = s.toLowerCase();
   
        if (isPalindrome(s))
            System.out.print("Yes");
        else
            System.out.print("No");
    }
}
Comment

Palindrome Program in Java.

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static boolean isPalindrome(String str)
    {
        // Initializing an empty string to store the reverse
        // of the original str
        String rev = "";
 
        // Initializing a new boolean variable for the
        // answer
        boolean ans = false;
 
        for (int i = str.length() - 1; i >= 0; i--) {
            rev = rev + str.charAt(i);
        }
 
        // Checking if both the strings are equal
        if (str.equals(rev)) {
            ans = true;
        }
        return ans;
    }
    public static void main(String[] args)
    {
        // Input string
        String str = "geeks";
 
        // Convert the string to lowercase
        str = str.toLowerCase();
        boolean A = isPalindrome(str);
        System.out.println(A);
    }
}
Comment

How to find the longest palindrome substring in a bigger string, in Java?

/*
	This implementation demonstrates how to 
	find the longest palindrome substring 
	in a given bigger string. 

	
	Let n be the length of the bigger string.

	Time complexity: O(n^2) 
	Space complexity: O(1)
*/
public class LongestPalindromeSubstring {

	public static void main(String[] args) {
		String s = "babad";
		System.out.println(longestPalindrome(s)); // bab
	}

	private static String longestPalindrome(String s) {
		if (s.length() == 1) {
			return s;
		}

		int maxLength = Integer.MIN_VALUE;
		String longestPalindrome = "";
		int n = s.length();

		for (int index = 0; index < n; index++) {
			for (int x = 0; index - x >= 0 && index + x < n; x++) {
				if (s.charAt(index - x) == s.charAt(index + x)) {
					int len = 2 * x + 1;
					if (len > maxLength) {
						maxLength = len;
						longestPalindrome = s.substring(index - x, index + x + 1);
					}
				} else {
					break;
				}
			}
		}

		for (int index = 0; index < n - 1; index++) {
			for (int x = 1; index - x + 1 >= 0 && index + x < n; x++) {
				if (s.charAt(index - x + 1) != s.charAt(index + x)) {
					break;
				} else {
					int len = 2 * x;
					if (len > maxLength) {
						maxLength = len;
						longestPalindrome = s.substring(index - x + 1, index + x + 1);
					}
				}
			}
		}

		return longestPalindrome;
	}
}
Comment

Longest Palindrome String in java

class Solution {
   String getLongestPalindrome(String s) {
       int n = s.length();
       int index  = 0, palindromeLength = 1;
       for (int i = 1; i < n; i++) {
           int left = i - 1, right = i;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           left = i - 1;
           right = i + 1;
           while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
               if(right - left + 1 > palindromeLength) {
                   index = left;
                   palindromeLength = right - left + 1;
               }
               left--;
               right++;
           }
           
       }
       String ans = "";
       for (int i = index; i < index + palindromeLength; i++) {
           ans += s.charAt(i);
       }
       return ans;
   }
}
Comment

palindrome in java

package com.candidjava; import java.util.Scanner; public class PalindromeUptoN{	public static void main(String[] args)	{		int n, b, rev = 0;		Scanner sc = new Scanner(System.in);		System.out.println("Enter the Palindrome No N:");		int N = sc.nextInt();		System.out.print("Palindrome numbers from 1 to N:");		for (int i = 1; i <= N; i++)		{			n = i;			while (n > 0)			{				b = n % 10;				rev = rev * 10 + b;				n = n / 10;			}			if (rev == i)			{				System.out.print(i + " ");			}			rev = 0;		}	} }
Comment

PREVIOUS NEXT
Code Example
Java :: detect view in scrollview android 
Java :: The larger and the smaller of the character “E” and the integer 71 java 
Java :: bytecode into source code 
Java :: tutorialedge working with docker 
Java :: android studio cannot resolve @nullable 
Java :: Spring Boot user registration and login REST API 
Java :: non access modifiers in java 
Java :: access modifier overloaded method 
Java :: Java Generic Functional Interface 
Java :: kubernetes java client create namespace 
Java :: python to java code conversion 
Java :: java.lang.ArrayIndexOutOfBoundsException: 4 
Java :: newinstance in java giving exception 
Java :: java try with multiple resources 
Java :: java runtime.getruntime().exec 
Java :: java hashtable 
Java :: is java pass by value or pass by reference 
Java :: how to set current item for spinner android 
Java :: adding an element to the end of a linked list java 
Java :: java enum values() 
Java :: calling a method in java 
Java :: how to find a int 
Java :: java font bold italic 
Java :: jgit clone in memory 
Java :: ciclo for para percorere duas listas java 
Sql :: postgresql remove not null constraint 
Sql :: sql finding longest and shortest names in a fleld 
Sql :: mysql how to reset primary key 
Sql :: display all databases 
Sql :: find largest table in mysql database 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =