Last active
February 15, 2019 13:07
-
-
Save OhadC/9335a58145a827dc7d2e64b2bc6818c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://www.youtube.com/watch?v=tj_sBZw9nS0 | |
| const isMatch = (s: string, p: string): boolean => { | |
| if (!s.length || !p.length) { | |
| return !s.length && !p.length; | |
| } | |
| if (p[1] === '*') { | |
| if (isMatch(s, p.substr(2))) return true; | |
| if (p[0] === '.') { | |
| for (let i = 0; i < s.length; i++) { | |
| if (isMatch(s.substr(i + 1), p.substr(2))) return true; | |
| } | |
| } | |
| else { | |
| for (let i = 0; s[i] === p[0]; i++) { | |
| if (isMatch(s.substr(i + 1), p.substr(2))) return true; | |
| } | |
| } | |
| return false | |
| } | |
| else if (p[0] === '.' || s[0] === p[0]) { | |
| return isMatch(s.substr(1), p.substr(1)); | |
| } | |
| } | |
| const tests = [ | |
| { s: 'aba', p: 'ab', shouldBe: false }, | |
| { s: 'aa', p: 'a*', shouldBe: true }, | |
| { s: 'ab', p: '.*', shouldBe: true }, | |
| { s: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab', p: '.*a.*.*b', shouldBe: true }, | |
| { s: 'aaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaab', p: 'a*b', shouldBe: false }, | |
| { s: 'ab', p: '.', shouldBe: false }, | |
| { s: 'aab', p: 'c*a*b', shouldBe: true }, | |
| { s: 'aaa', p: 'a*', shouldBe: true }, | |
| { s: 'aaa', p: 'a.a', shouldBe: true } | |
| ]; | |
| tests.forEach(test => { | |
| const res = isMatch(test.s, test.p); | |
| const isSucceed = test.shouldBe === res; | |
| const isSucceedText = isSucceed ? 'success' : 'fail'; | |
| console.log(`${isSucceedText}: s: ${test.s}, p: ${test.p}, res: ${res}, shouldBe: ${test.shouldBe}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment