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. 如何删除tomcat下的一目

    不知道我有没有把问题想简单了,是不是应该把webapps下对应的文件夹删了就可以了. work下面对应的也删掉 这个取决于你在tomcat下发布那个项目的方式. 首先是工程的根目录要删除,然后是工程相 ...

  2. Chapter 1 First Sight——20

    After two classes, I started to recognize several of the faces in each class. 两节课之后,我开始记住了每节课的那几张脸. ...

  3. 完整的 HTML 4 + HTML 5 实体参考手册

    1 完整的 HTML 4 + HTML 5 实体参考手册 http://www.runoob.com/charsets/html-charsets.html 1 下表中的所有实体都能在所有的浏览器中正 ...

  4. 设计模式---Manager(管理器)

    设计模式之美:Manager(管理器) 索引 意图 结构 参与者 适用性 效果 实现 实现方式(一):Manager 模式的示例实现. 意图 将对一个类的所有对象的管理封装到一个单独的管理器类中. 这 ...

  5. C#实现拷贝对象

    大家都知道,在C#中变量的存储分为值类型和引用类型两种,而值类型和引用类型在数值变化是产生的后果是不一样的,值类型我们可以轻松实现数值的拷贝,那么引用类型呢,在对象拷贝上存在着一定的难度.     下 ...

  6. struts2语法--error页面如何捕获?

    如果地址栏输入了不带后缀或者action为后缀, 不存在的页面跳转到error.jsp: struts.xml配置" <package name="default" ...

  7. SQL Server 2008登录问题(错误 233和18456)解决方法

    今天使用 SQLSERVER2008 先遇到了233 错误,后又遇到了 18456 ,从网上找到了解决方法,具体如下: 问题一 : 已成功与服务器建立连接,但是在登录过程中发生错取.(provider ...

  8. php通过token验证表单重复提交

    PHP防止重复提交表单 2016-11-08 轻松学PHP 我们提交表单的时候,不能忽视的一个限制是防止用户重复提交表单,因为有可能用户连续点击了提交按钮或者是攻击者恶意提交数据,那么我们在提交数据后 ...

  9. Mercurial hg web server的配置

    在windows下安装tortoisehg-1.0.3-hg-1.5.3-x64.exe的版本控制工具后,克隆建立中心库后,启动web server,其他分库可以连接中心库进行pull但无法push. ...

  10. JSON & XML 简析

    转载自:http://my.oschina.net/aofe/blog/269260 JSON: XML: JSON格式说明: HTML & XML 的对比 HTML: XML: HTML5新 ...