Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case;

Ex: "A@,b123a", equals "Aba", should output 4: "A", "b", "a", "Aba"

Spread from center: Like LC 5: longest palindromic substring

 package fb;

 public class Palindrome {
public int countSubStr(String input) {
if (input==null || input.length()==0) return 0;
int res = 0;
for (int i=0; i<input.length(); i++) {
if (!isAlpha(input, i)) continue;
res += helper(input, i, i);
if (i < input.length()-1) res += helper(input, i, i+1);
}
return res;
} public int helper(String s, int l, int r) {
int res = 0;
while (l>=0 && r<s.length()) {
while (l>=0 && !isAlpha(s, l)) l--;
while (r<s.length() && !isAlpha(s, r)) r++;
if (l<0 || r>=s.length() || !isSame(s, l, r)) break;
else {
res++;
l--;
r++;
}
}
return res;
} public boolean isAlpha(String s, int i) {
char c = s.charAt(i);
if (c>='a' && c<='z' || c>='A' && c<='Z') return true;
return false;
} public boolean isSame(String s, int l, int r) {
char ll = s.charAt(l);
char rr = s.charAt(r);
ll = Character.isUpperCase(ll)? (char)(ll-'A'+'a') : ll;
rr = Character.isUpperCase(rr)? (char)(rr-'A'+'a') : rr;
return ll==rr;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Palindrome sol = new Palindrome();
String input = "A@,b123a";
//String input = "Aba";
int res = sol.countSubStr(input);
System.out.println(res); } }

FB面经 Prepare: All Palindromic Substrings的更多相关文章

  1. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  2. [Swift]LeetCode647. 回文子串 | Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  3. 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  4. Leetcode 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  5. LeetCode——Palindromic Substrings

    Question Given a string, your task is to count how many palindromic substrings in this string. The s ...

  6. 647. Palindromic Substrings 互文的子字符串

    [抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...

  7. 【Leetcode】647. Palindromic Substrings

    Description Given a string, your task is to count how many palindromic substrings in this string. Th ...

  8. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  9. LeetCode 647. 回文子串(Palindromic Substrings)

    647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...

随机推荐

  1. RedHat Enterprise Linux AS4&5 安装gcc过程

    三.Gcc安装方法(redhat 4): 一.安装步骤 1.使用which gcc命令查看gcc是否安装安装 2.如若没有安装则下载如下安装包,所需安装包如下 一共需要拷贝以下五个安装包: binut ...

  2. 仿照微信的界面,即ViewPager+Fragment的结合使用

    主布局文件: android:drawableTop="@drawable/weixin_bg"用的是状态选择器,所以要写4个状态选择器,图片的 <RelativeLayou ...

  3. 如何获取url访问历史记录

    在院里的群里,有人问了这么一个问题: A页面提交表单到B页面,然后在B页面点了后退,如果在A页面上判断是直接访问的还是后退进去的呢?我不想改B页面. 于是乎本着热心人的想法,我就帮他搞了搞,首先我想到 ...

  4. 快速部署Python应用:Nginx+uWSGI配置详解

    在PHP里,最方便的就是deployment了,只要把php文件丢到支持PHP的路径里面,然后访问那个路径就能使用了:无论给主机添加多少PHP应用,只要把目录改好就没你的事了,完全不用关心php-cg ...

  5. jq中的evet.target

    1.this和event.target的区别: js中事件是会冒泡的,所以this是可以变化的,但event.target不会变化,它永远是直接接受事件的目标DOM元素: 2.this和event.t ...

  6. g1gc

    http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All http://blog.csdn.net/renfufe ...

  7. FusionCharts导出图表常见问题(FAQ)汇总---FusionCharts常见问题大全

    在前面几篇文章中,我们介绍了FusionCharts生成Flash图表常见问题FAQ以及使用中的一些常见报错及调试/解决方法.本文继续介绍FusionCharts导出图表时的一些常见问题(FAQ). ...

  8. 使用traceview进行Android性能测试(转)

    使用traceview进行Android性能测试(转) 使用traceview进行Android性能测试  原文链接:http://www.cnblogs.com/Android-and-androi ...

  9. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

  10. USACO Section 1.2 Milking Cows 解题报告

    题目 题目描述 有3个农夫每天早上五点钟便起床去挤牛奶,现在第一个农夫挤牛奶的时刻为300(五点钟之后的第300个分钟开始),1000的时候结束.第二个农夫从700开始,1200结束.最后一个农夫从1 ...