【LintCode】算法题 1443. 最长AB子串

 

描述

 

 

给你一个只由字母'A''B'组成的字符串s,找一个最长的子串,要求这个子串里面'A''B'的数目相等,输出该子串的长度。

  • 这个子串可以为空。
  • s的长度n满足 2<=n<=1000000

 

样例

 

 

给定s="ABAAABBBA",返回8

解释:
子串 s[0,7] 和子串 s[1,8] 满足条件,长度为 8。

给定s="AAAAAA",返回0

解释:
s 中除了空字串,不存在 'A' 和 'B' 数目相等的子串。

 

Java 解法

 

public class Solution {
    /**
     * @param S: a String consists of a and b
     * @return: the longest of the longest string that meets the condition
     */
    public int getAns(String S) {
        // Write your code here
        int l = S.length();
        int[] numA = new int[l+1];
        int[] numB = new int[l+1];
        int a = 0, b = 0;
        
        for(int i = 0; i < l; i++) {
            if(S.charAt(i) == 'A') a++;
            else b++;
            numA[i+1] = a;
            numB[i+1] = b;
        }
        
        if(a == 0 || b == 0) return 0;
        
        int res = a > b? a : b;
        for(res *= 2; res > 0; res -= 2) {
            for(int i = res; i < l+1; i++) {
                if(numA[i] - numA[i-res]== numB[i] - numB[i-res]) {
                    return res;
                }
            }
        }
        
        return 0;
    }
}

 

欢迎关注我的知乎专栏【数据池塘】,专注于分享机器学习、数据挖掘相关内容:https://zhuanlan.zhihu.com/datapool

 

⬇️ 扫描下方二维码关注公众号【数据池塘】 ⬇️

回复【算法】,获取最全面的机器学习算法网络图:

回答: 最长回文子串可以通过两种方法来实现。第一种是使用中心扩展法,代码如下: ```python class Solution: def check(self, s, l, r): while l >= 0 and r < len(s) and s[l == s[r]: l -= 1 r += 1 return l + 1, r - 1 def longestPalindrome(self, s: str) -> str: start, end = 0, 0 for x in range(len(s)): l1, r1 = self.check(s, x, x) l2, r2 = self.check(s, x, x + 1) if r1 - l1 > end - start: start, end = l1, r1 if r2 - l2 > end - start: start, end = l2, r2 return s[start:end+1] ``` 第二种方法是使用动态规划,代码如下: ```python class Solution: def longestPalindrome(self, s: str) -> str: res = '' for i in range(len(s)): start = max(0, i - len(res) - 1) temp = s[start:i+1] if temp == temp[::-1]: res = temp else: temp = temp<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [5. 最长回文子串(Python 实现)](https://blog.csdn.net/d_l_w_d_l_w/article/details/118861851)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [LeetCode(Python3)5.最长回文子串](https://blog.csdn.net/weixin_52593484/article/details/124718655)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [力扣 (LeetCode)刷题笔记5.最长回文子串 python](https://blog.csdn.net/qq_44672855/article/details/115339324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风控大鱼

如果帮到了您,请我喝杯咖啡吧~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值