FB面经 Prepare: All Palindromic Substrings
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的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- [Swift]LeetCode647. 回文子串 | Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- 647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- Leetcode 647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode——Palindromic Substrings
Question Given a string, your task is to count how many palindromic substrings in this string. The s ...
- 647. Palindromic Substrings 互文的子字符串
[抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...
- 【Leetcode】647. Palindromic Substrings
Description Given a string, your task is to count how many palindromic substrings in this string. Th ...
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode 647. 回文子串(Palindromic Substrings)
647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...
随机推荐
- hashmap源码
Java里各种基础容器的实现都是链表外加引用的形式.So,hashmap也不例外. 那小考虑下:hashmap是怎么产生的呢? 常用的两种数据结构数组和链表各有优劣,数组寻址容易,插入和删除困难:而链 ...
- ormlite 删除操作
ormlite删除操作 DeleteBuilder<TransferDetailDtl, Integer> deleteBuilder = mRawDao.deleteBuilder(); ...
- android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable
第一个感叹号 是跟你说 让你把Imageview 和textview 结合起来 只用 textview textview有个属性叫 android:drawable...(top/bottom/.. ...
- 将项目同时托管到Github和Git@OSC
http://my.oschina.net/GIIoOS/blog/404555?fromerr=KHvn8UKH 摘要 Github是最大的git代码托管平台,GIT@OSC是国内最大的git代码 ...
- java工程开发之图形化界面之(第五课)
下面我们将: 一)更加完整的解释Graphics类 二)使用方法来更清晰的重新编写前面图形小应用程序之一 三)介绍一些其他的绘图的方法 四)介绍方法init,它是类似于paint但是用于不同用途的另一 ...
- 鉴客 C# 抓取页面(带认证)
1. [代码][C#]代码 01 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(""); 02 re ...
- angular Jsonp的坑
angular 为了解决跨域问题 一些第三方接口会提供jsonp来调用,需要使用callback=JSON_CALLBACK来处理 这个时候问题来了,有些借口是不支持callback里面带有点语法的, ...
- mystring c++ 自己的string 封装
1 /************************************************************************* > File Name: mystrin ...
- (转)mahout中k-means例子的运行
首先简单说明下,mahout下处理的文件必须是SequenceFile格式的,所以需要把txtfile转换成sequenceFile.SequenceFile是hadoop中的一个类,允 ...
- java中的序列化流和反序列化流
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px SimSun; color: #4e9072 } 序列化流:把对象按照流一样的方式存入文本文件 ...