string match 字符串匹配
作者:互联网
You are given a string s
. You can convert s
to a palindrome by adding characters in front of it.
Return the shortest palindrome you can find by performing this transformation.
Example 1:
Input: s = "aacecaaa" Output: "aaacecaaa"
Example 2:
Input: s = "abcd" Output: "dcbabcd"
Constraints:
0 <= s.length <= 5 * 104
s
consists of lowercase English letters only.
思路:首先如何满足palindrom,从开始数,找到最长的palindrome,然后把剩余不是的部分倒转拼到左侧即实现了palindrome
解法1:暴力尝试所有情况,时间复杂读为O(N2)
解法一就不写了
解法2: 使用rolling hash,如果是palindrome的话,从左右两侧计算hash值应该是同一个值
因此,我们可以从第一个字母开始逐步向后计算hash值,一个维护从前到后的hash值,一个维护从后向前的hash值
class Solution { public String shortestPalindrome(String s) { if("".equals(s)) return s; int sum = 0, reverse = 0; int pos = 0; int power = 1; int mod = 19999999; for(int i=0;i<s.length();i++){ int c = s.charAt(i)-'a'; sum = (sum*26+c)%mod; reverse = (c*power+reverse)%mod; power = power*26%mod; if( sum==reverse && isParlindrom(s.substring(0,i+1)) ){ pos = i; } } return (new StringBuilder(s.substring(pos+1)).reverse()).toString()+s; } private boolean isParlindrom(String s){ int left = 0,right = s.length()-1; while(left<right){ if(s.charAt(left) == s.charAt(right)){ left++;right--; } else return false; } return true; } }647. Palindromic Substrings Medium
Given a string s
, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Example 1:
Input: s = "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Constraints:
1 <= s.length <= 1000
s
consists of lowercase English letters.
解法: 只需要简单的从左侧开始, 以当前字母和当前两个字母为中心向两侧判定计算palindrome
class Solution { int count = 0; public int countSubstrings(String s) { int len = s.length();
//以逐个字母为中心进行计算 for(int i=0;i<len;i++){ valid(s,i,i);//计算奇数情况 if(i<len-1) valid(s,i,i+1);//计算偶数个情况 } return count; } public void valid(String s, int left,int right){ int len = s.length(); while(left>=0 && right<len){ if(s.charAt(left)==s.charAt(right)){ left--;right++; count++; } else break; } } }
1216. Valid Palindrome III Hard
Given a string s
and an integer k
, return true
if s
is a k
-palindrome.
A string is k
-palindrome if it can be transformed into a palindrome by removing at most k
characters from it.
Example 1:
Input: s = "abcdeca", k = 2 Output: true Explanation: Remove 'b' and 'e' characters.
Example 2:
Input: s = "abbababa", k = 1 Output: true
Constraints:
1 <= s.length <= 1000
s
consists of only lowercase English letters.1 <= k <= s.length
解法:lcs,实际这个题目可以转化为longest common subsequence 问题,我们只要得到这个字符串与其反转后字符串的lcs,那么剩余的就是需要删除的个数
class Solution { public boolean isValidPalindrome(String s, int k) {
//得到lcs int max = dfs(s,0,s.length()-1,new Integer[s.length()][s.length()]); return s.length()-max<=k;//长度-lcs 即为需要删除的个数 } private int dfs(String s,int left,int right,Integer[][] mem){ if(left >= s.length() || right<0) return 0; if(mem[left][right]!=null) return mem[left][right]; if(s.charAt(left) == s.charAt(right)){ mem[left][right] = dfs(s, left+1, right-1,mem)+1; } else{ mem[left][right] = Math.max(dfs(s, left+1, right,mem),dfs(s, left, right-1,mem)); } return mem[left][right]; } }
标签:palindrome,string,int,length,字符串,Input,Example,match 来源: https://www.cnblogs.com/cynrjy/p/16369454.html