From 68bdbfb0a58560041f2fd7313b87ccf5ebd28777 Mon Sep 17 00:00:00 2001 From: Yashvardhan Singh <68675629+PythonicBoat@users.noreply.github.com> Date: Thu, 10 Nov 2022 06:54:15 +0530 Subject: [PATCH] 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 --- leetcode/DIRECTORY.md | 1 + leetcode/src/10.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 leetcode/src/10.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 0de9c220..1283f175 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -12,6 +12,7 @@ | 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 | | 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 | | 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 | diff --git a/leetcode/src/10.c b/leetcode/src/10.c new file mode 100644 index 00000000..26ed6a3b --- /dev/null +++ b/leetcode/src/10.c @@ -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; +}