Implement regular expression matching with support for '.' and '*'.

DP:

 public class Solution {
public boolean isMatch2(String s, String p) {
int starCnt = 0;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
starCnt++;
}
} boolean[] star = new boolean[p.length() - starCnt];
StringBuilder temp = new StringBuilder(p.length() - starCnt);
int index = -1;
for (int i = 0; i < p.length(); i++) {
if (p.charAt(i) == '*') {
star[index] = true;
} else {
temp.append(p.charAt(i));
star[++index] = false;
}
}
String r = temp.toString(); boolean[] lastRow = new boolean[s.length() + 1];
boolean[] curRow = new boolean[s.length() + 1];
boolean[] tempRow; lastRow[0] = true;
for (int i = 0; i < r.length(); i++) {
if (star[i] && lastRow[0]) {
curRow[0] = true;
} else {
curRow[0] = false;
} for (int j = 0; j < s.length(); j++) {
if (!star[i]) {
if ((r.charAt(i) == '.' || r.charAt(i) == s.charAt(j)) && lastRow[j]) {
curRow[j + 1] = true;
} else {
curRow[j + 1] = false;
}
} else {
if (lastRow[j + 1]) {
curRow[j + 1] = true;
} else if (lastRow[j] || curRow[j]) {
if (r.charAt(i) == '.' || r.charAt(i) == s.charAt(j)) {
curRow[j + 1] = true;
} else {
curRow[j + 1] = false;
}
} else {
curRow[j + 1] = false;
}
}
} tempRow = lastRow;
lastRow = curRow;
curRow = tempRow;
} return lastRow[lastRow.length - 1];
}
}

最新文章

  1. Transaction Replication6:Transaction cleanup
  2. SQL Server中Rowcount与@@Rowcount的用法 和set nocount on 也会更新@@Rowcount
  3. svn没有对号等符号的问题
  4. Python学习笔记10
  5. 本人常用的Linux bash快捷键(持续更新)
  6. SVN Unable to connect to a repository at URL问题解决
  7. JQuery基础DOM操作
  8. m3u8
  9. hdu 5277 YJC counts stars 暴力
  10. CSS3 新特性 开放字体格式WOFF
  11. 06-OC分类、协议、ARC
  12. Hibernate三种状态及生命周期
  13. 打开safari开发者选项
  14. PowerDesigner的安装
  15. eclipse 安装 ndk 组件
  16. BZOJ5324 JXOI2018守卫(区间dp)
  17. struts2请求两次即action方法执行两次
  18. 4.9版本的linux内核中实时时钟芯片pcf85263的驱动源码在哪里
  19. 橱窗布置(Flower)(动规)
  20. K3Cloud调用存储过程

热门文章

  1. 【转】slice,substr和substring的区别
  2. 【LeetCode】Sort Colors
  3. POJ 3067 原来是树状数组--真的涨姿势
  4. -webkit-appearance、sselect
  5. Java语言的安全性的体现
  6. spring HandlerInterceptorAdapter拦截ajax异步请求,报错ERR_INCOMPLETE_CHUNKED_ENCODING
  7. HashMap归档-超越昨天的自己系列
  8. LoadRunner迭代与并发的理解
  9. lambda表达式
  10. 20160405互联网新闻&lt;来自涛涛大产品&gt;