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:
Yashvardhan Singh 2022-11-10 06:54:15 +05:30 committed by GitHub
parent 9101ccd27c
commit 68bdbfb0a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 0 deletions

View File

@ -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
View 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;
}