Java for LeetCode 214 Shortest Palindrome

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.

For example:

Given "aacecaaa", return "aaacecaaa".

Given "abcd", return "dcbabcd".

解题思路:

本题最简单的思路是从后往前判断子串是否是回文串,然后把后面的弄到前面即可,不过这样仅仅能擦边通过测试,最高效的当然是KMP算法了,

KMP可以参考Java for LeetCode 028 Implement strStr()

JAVA实现如下;

    public String shortestPalindrome(String s) {
        for(int i=s.length();i>=1;i--)
        	if(isPalindrome(s.substring(0, i)))
        		return new StringBuilder(s.substring(i)).reverse()+s;
        return "";
    }
    static boolean isPalindrome(String s){
    	int left=0,right=s.length()-1;
    	while(left<right)
    		if(s.charAt(left++)!=s.charAt(right--))
    			return false;
    	return true;
    }
时间: 2024-12-29 09:52:50

Java for LeetCode 214 Shortest Palindrome的相关文章

[LeetCode][JavaScript]Shortest Palindrome

Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", retur

Java for LeetCode 125 Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. 解题思路: 注意题目,忽略大小写,忽略非字母或数字,JA

214. Shortest Palindrome

一.题目 1.审题 2.分析 给出一个字符串,在此字符串前边添加字符,使得其成为一个回文,求添加最少字符后,所形成的回文. 二.解答 1.思路: ①.为了处理回文字符数为奇数和偶数的问题,先在字符串 s 的每一个字符之间插入字符 '#',并将每个字符放入一个 List 中 ②.下标 index 依次从 1 到 mid,定义两个指针 left = index - 1.right = index + 1: 比较 left .right 所指元素是否相等,若不相等,则 index 向后移动,若相等,则

[LeetCode] 243. Shortest Word Distance 最短单词距离

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example,Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. G

[LeetCode-JAVA] Shortest Palindrome

题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaa

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge