mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
feat: add LeetCode problem 10 (#1033)
* updated readme * Create 10.c * Update README.md * added documentation * chore: apply suggestions from code review * Update DIRECTORY.md Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
9101ccd27c
commit
68bdbfb0a5
@ -12,6 +12,7 @@
|
|||||||
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c) | Easy |
|
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C](./src/7.c) | Easy |
|
||||||
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c) | Medium |
|
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi) | [C](./src/8.c) | Medium |
|
||||||
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c) | Easy |
|
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C](./src/9.c) | Easy |
|
||||||
|
| 10 | [Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/) | [C](./src/10.c) | Hard |
|
||||||
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [C](./src/11.c) | Medium |
|
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [C](./src/11.c) | Medium |
|
||||||
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [C](./src/12.c) | Medium |
|
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman) | [C](./src/12.c) | Medium |
|
||||||
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c) | Easy |
|
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C](./src/13.c) | Easy |
|
||||||
|
59
leetcode/src/10.c
Normal file
59
leetcode/src/10.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
Prompt:
|
||||||
|
|
||||||
|
Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where:
|
||||||
|
- '.' Matches any single character.
|
||||||
|
- '*' Matches zero or more of the preceding element.
|
||||||
|
The matching should cover the entire input string (not partial).
|
||||||
|
|
||||||
|
Constraints:
|
||||||
|
|
||||||
|
1 <= s.length <= 20
|
||||||
|
1 <= p.length <= 30
|
||||||
|
s contains only lowercase English letters.
|
||||||
|
p contains only lowercase English letters, '.', and '*'.
|
||||||
|
|
||||||
|
It is guaranteed for each appearance of the character '*', there will be a previous valid character to match.
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool isMatch(char* s, char* p);
|
||||||
|
bool matchStar(char ch, char* s, char* p);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Uses Rob pikes Regexp matcher - https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
|
||||||
|
Implementation:
|
||||||
|
// match: search for regexp anywhere in text
|
||||||
|
int match(char *regexp, char *text)
|
||||||
|
{
|
||||||
|
if (regexp[0] == '^')
|
||||||
|
return matchhere(regexp+1, text);
|
||||||
|
do {
|
||||||
|
if (matchhere(regexp, text))
|
||||||
|
return 1;
|
||||||
|
} while (*text++ != '\0');
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
bool matchStar(char ch, char* s, char* p) {
|
||||||
|
do {
|
||||||
|
if (isMatch(s, p))
|
||||||
|
return true;
|
||||||
|
} while (*s != '\0' && (*s++ == ch || ch == '.'));
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isMatch(char* s, char* p) {
|
||||||
|
if (*p == '\0')
|
||||||
|
return *s == '\0';
|
||||||
|
|
||||||
|
if (p[1] == '*')
|
||||||
|
return matchStar(p[0], s, p + 2);
|
||||||
|
|
||||||
|
if (*s != '\0' && (p[0] == '.' || *p == *s)) {
|
||||||
|
return isMatch(s + 1, p + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user