ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

string match 字符串匹配

2022-06-13 03:00:43  阅读:147  来源: 互联网

标签:palindrome string int length 字符串 Input Example match


214. Shortest Palindrome Hard

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有