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. elasticsearch 索引优化

    ES索引优化篇主要从两个方面解决问题,一是索引数据过程:二是检索过程.  索引数据过程我在上面几篇文章中有提到怎么创建索引和导入数据,但是大家可能会遇到索引数据比较慢的过程.其实明白索引的原理就可以有 ...

  2. cocos2d3.8.1 使用prebuild提升发布android速度

    1.生成cocos prebuild库 cocos gen-libs -m debug或 cocos gen-libs -m release 2.使用命令创建test项目 cocos new test ...

  3. android 5.0新特性学习--CardView

    CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. 官网地址:https://de ...

  4. PAT1006

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  5. 团队开发里频繁使用 git rebase 来保持树的整洁好吗?

    用了以后, 树可以非常清晰, 某种程度上便于追踪, 但是 push --force 就多多了,不用呢, 合并没有远程仓库被修改的麻烦, 可是追踪又不清晰... git rebase是对commit h ...

  6. win10系统安装oracle11g时遇到INS-13001环境不满足最低要求

    升级win10系统之后,需要重新安装Oracle,因为在安装Oralce11g时,使用64位的会出现各种不兼容问题,我每次安装都是使用32位的数据库. 在安装时点击setup.exe之后,出现了:[I ...

  7. typedef void far *LPVOID 的具体定义

    首先这里的far,在32位系统已经废除不用了.它是C/C++语言在16位系统中用以标明指针是个远指针的修饰符. 远指针是说指针所指向的地址已经超出了64K(2的十六次方),所以需要使用DS加偏移量的方 ...

  8. lpr

    http://flatline.cs.washington.edu/orgs/acm/tutorials/printing/index.html

  9. mysql链接表,connection string, federated engine

    http://database.51cto.com/art/201011/234561.htm

  10. javascript之定义函数时 this 和prototype区别

    注:原文 http://www.2cto.com/kf/201406/307790.html 这里作为学习,写在这里 在面向对象的方式编写js脚本时,定义实例的方法主要有两种:this.XXX = f ...