尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
YuanBao    LeetCode 10.Regular Expression Matching Java Implement
Here is a standard Dynamic Programming solution for LeetCode 10 (Regular Expression Matching) in Java.ApproachWe use a 2D boolean array“dp[i][j]” which represents whether the first“i” characters of the string“s” match the first“j” characters of the pattern“p”.Key Rules:Base Case:“dp[0][0] true” (empty matches empty).Pattern ends with“: The” can match zero of the preceding element (“dp[i][j-2]”) OR one/more if the preceding character matches (“dp[i-1][j]”).Normal match /“‘.’”: If characters match or pattern is“‘.’”, carry over the previous state (“dp[i-1][j-1]”).Java Implementationclass Solution {public boolean isMatch(String s, String p) {int m s.length();int n p.length();// dp[i][j] true if first i chars of s match first j chars of p boolean[][] dp new boolean[m 1][n 1]; // 1. Base case: empty string matches empty pattern dp[0][0] true; // 2. Handle patterns like a*, a*b*, a*b*c* matching an empty string for (int j 2; j n; j) { if (p.charAt(j - 1) *) { dp[0][j] dp[0][j - 2]; } } // 3. Fill the DP table for (int i 1; i m; i) { for (int j 1; j n; j) { char currentCharS s.charAt(i - 1); char currentCharP p.charAt(j - 1); // Case A: Current characters match or pattern has . if (currentCharP currentCharS || currentCharP .) { dp[i][j] dp[i - 1][j - 1]; } // Case B: Current pattern character is * else if (currentCharP *) { // * matches zero of the preceding element dp[i][j] dp[i][j - 2]; // * matches one or more of the preceding element // Check if the character before * matches current string char char charBeforeStar p.charAt(j - 2); if (charBeforeStar currentCharS || charBeforeStar .) { dp[i][j] dp[i][j] || dp[i - 1][j]; } } // Case C: Characters dont match else { dp[i][j] false; } } } return dp[m][n]; }}Complexity AnalysisTime Complexity: O(m \times n) , where m is the length of“s” and nis the length ofp. We fill a table of size (m1) \times (n1)$.Space Complexity: O(m \times n) for the DP table. (This can be optimized to O(n) using a 1D array if needed.)Test Cases“s”“p” Output““aa””““a””“false”““aa””““a*””“true”““ab””“”.*“”“true”““aab””““cab””“true”““mississippi””““misisp*.””“false”Would you like me to explain a specific part of the logic in more detail, or provide the space-optimized (1D DP) version?
RELATED

相关推荐

C++ 泛型世界的两块拼图:容器适配器与仿函数

C++ 泛型世界的两块拼图:容器适配器与仿函数

🏆浮世尘弦:个人主页 ⭐个人专栏:《C语言》、《数据结构与算法》、《C》 💎:非淡薄无以明志,非宁静无以致远 前言 前言:stack和queue是C中的容器适配器,不是自己实现存储的新容器&am…

📅 2026/11/15 13:34:15
【minio】#4 | MinIO API 文件操作

【minio】#4 | MinIO API 文件操作

一、文件上传(PutObject / FPutObject)MinIO 上传特性:文件超过 128MB 自动分片传输;单文件上限 5TB。 模拟文件夹原理:MinIO 本身没有文件夹概念,通过 ObjectKey 前缀模拟目录,如0114/ssh隧道命…

📅 2026/11/25 17:37:45
(论文速读)LogSAD:无需训练的结构异常与逻辑异常统一检测

(论文速读)LogSAD:无需训练的结构异常与逻辑异常统一检测

论文题目:Towards Training-free Anomaly Detection with Vision and Language Foundation Models(迈向基于视觉与语言基础模型的免训练异常检测) 会议:CVPR 2025 摘要:异常检测在工业质量检测等真实场景中具有重要价…

📅 2026/11/15 13:34:15
MORE NEWS

更多资讯

📰

广西免费培训的电工证实操考试心里没底怕挂科?2026河南最新政策全解析

广西免费培训的电工证实操考试心里没底怕挂科?2026河南最新政策全解析 实操考试心里没底怕挂科?你不是一个人。尤其是准备考【广西免费培训的电工证】的小伙伴,很多都担心操作不规范、流程不熟,一上手就露馅。别慌,2026年河南最新政策已经出来了…

📰

宁德哪里考高压电工证2026河南最新政策解读

宁德哪里考高压电工证2026河南最新政策解读 急需特种作业证才能上岗找工作,这个现实问题正在越来越多的从业者面前凸显。尤其是在高压电工证这种高门槛、高风险的特种作业领域,证书不仅是上岗的“通行证”,更是企业招聘时的重要筛选标准。2026年河…

📰

来宾安监局电工证换证流程全解,实操考试心里没底怕挂科?多少钱搞定?

来宾安监局电工证换证流程全解,实操考试心里没底怕挂科?多少钱搞定? 实操考试心里没底怕挂科?现在考电工证,很多人最怕的就是实操部分,一不小心就挂科,耽误时间又浪费钱。特别是对于刚入行的新人,比如周口初次报考的小伙伴,更是对【来宾安监局电工证…

📰

军登特种工雨靴多少钱模拟考试题库

低压电工证怎么考?学历不高怕报不上名?官方报名入口全解析 你是不是也担心自己学历不够,考不了低压电工证?其实现在考低压电工证并不难,关键是要找对【官方报名入口】,了解最新政策,避免踩坑。尤其是像你这样担心学历不够的,更要清楚低压电工证的报考…

📰

上饶哪里可以办电工证实操考试心里没底怕挂科?考前押题助你稳过

上饶哪里可以办电工证实操考试心里没底怕挂科?考前押题助你稳过 实操考试心里没底怕挂科?你不是一个人。很多人在准备电工证考试时,最怕的就是实操部分,一不小心就挂科,不仅浪费时间和金钱,还影响后续的职业发展。尤其是在上饶,考电工证的流程、机构选…

📰

福州高压电工证查询网上查不到?工地太忙根本没时间复习考试

福州高压电工证查询网上查不到?工地太忙根本没时间复习考试 工地太忙根本没时间复习考试,高压电工证网上查询又卡壳,这事儿我太懂了。我之前在河南某工地做电工,天天跑现场、扛设备,连抽空看个手机的时间都没有,结果高压电工证到期了,一查发现网上查不…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬