Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb6f4e08

Browse files
committed
ismatch
1 parente058939 commitb6f4e08

File tree

2 files changed

+1628
-0
lines changed

2 files changed

+1628
-0
lines changed

‎string/isMatch.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
packageAlgorithms.string;
2+
3+
publicclassisMatch {
4+
publicbooleanisMatch(Strings,Stringp) {
5+
if (s ==null ||p ==null) {
6+
returnfalse;
7+
}
8+
9+
returnisMatchRec(s,p,0,0);
10+
}
11+
12+
publicbooleanisMatchRec(Strings,Stringp,intindexS,intindexP) {
13+
intlenS =s.length();
14+
intlenP =p.length();
15+
16+
// we get to the end of the string.
17+
if (indexP ==lenP) {
18+
returnindexS ==lenS;
19+
}
20+
21+
// only 1 match character left.
22+
if (indexP ==lenP -1) {
23+
// String should also have 1 character left.
24+
returnindexS ==lenS -1 &&isMatchChar(s.charAt(indexS),p.charAt(indexP));
25+
}
26+
27+
// At lease 2 match character left
28+
if (p.charAt(indexP +1) =='*') {
29+
// match 0;
30+
if (isMatchRec(s,p,indexS,indexP +2)) {
31+
returntrue;
32+
}
33+
34+
// we can match 0 or more.
35+
for (inti =indexS;i <lenS;i++) {
36+
// match once or more.
37+
if (!isMatchChar(s.charAt(i),p.charAt(indexP))) {
38+
returnfalse;
39+
}
40+
41+
if (isMatchRec(s,p,i +1,indexP +2)) {
42+
returntrue;
43+
}
44+
}
45+
46+
// if any of them does not match, just return false.
47+
returnfalse;
48+
}
49+
50+
// match current character and the left string.
51+
returnindexS <lenS
52+
&&isMatchChar(s.charAt(indexS),p.charAt(indexP))
53+
&&isMatchRec(s,p,indexS +1,indexP +1);
54+
}
55+
56+
publicbooleanisMatchChar(chars,charp) {
57+
if (p =='*') {
58+
returnfalse;
59+
}
60+
61+
if (s ==p ||p =='.') {
62+
returntrue;
63+
}
64+
65+
returnfalse;
66+
}
67+
68+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp