首页 > 代码库 > LeetCode 44 Wildcard Matching(字符串匹配问题)
LeetCode 44 Wildcard Matching(字符串匹配问题)
题目链接:https://leetcode.com/problems/wildcard-matching/?tab=Description
‘?‘ Matches any single character.‘*‘ Matches any sequence of characters (including the empty sequence).
字符串匹配问题。如上所示:其中 ‘ ?’ 可以匹配任何一个单字符 ’ * ‘ 可以匹配任意长度的字符包括空字符串
给定字符串s,和字符串p。判断按照以上规则,字符串p是否能够匹配字符串s
参考代码:
package leetcode_50;/*** * * @author pengfei_zheng * 字符串匹配问题 */public class Solution44 { public boolean isMatch(String s, String p) { int sp = 0, pp = 0, match = 0, starIdx = -1; while (sp < s.length()){ if (pp < p.length() && (p.charAt(pp) == ‘?‘ || s.charAt(sp) == p.charAt(pp))){ sp++; pp++; } else if (pp < p.length() && p.charAt(pp) == ‘*‘){ starIdx = pp; match = sp; pp++; } else if (starIdx != -1){ pp = starIdx + 1; match++; sp = match; } else return false; } while (pp < p.length() && p.charAt(pp) == ‘*‘) pp++; return pp == p.length(); }}
LeetCode 44 Wildcard Matching(字符串匹配问题)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。