Skip to content

Commit 81ee6ab

Browse files
committed
10 - Regular Expression Matching
1 parent da90e74 commit 81ee6ab

File tree

3 files changed

+279
-0
lines changed

3 files changed

+279
-0
lines changed

10-regularExpressionMatching.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
var isMatch = function (s, p) {
2+
let a = 0;
3+
let b = 0;
4+
let lastMatch = {};
5+
6+
const patternTransform = transformPatternString(p);
7+
const noStars = patternTransform.filter((patternObj) => !patternObj.star);
8+
9+
let cont = true;
10+
11+
if (noStars.length && !preliminaryCheck(s, noStars)) return false;
12+
13+
while (cont) {
14+
const stringLetter = s[a];
15+
let patternObj = patternTransform[b];
16+
17+
console.log(stringLetter);
18+
console.log(patternObj);
19+
console.log(a, b);
20+
21+
if (!patternObj) {
22+
return a === s.length;
23+
}
24+
25+
if (
26+
patternObj.letter === lastMatch.letter &&
27+
!patternObj.star &&
28+
lastMatch.star
29+
) {
30+
b++;
31+
continue;
32+
}
33+
34+
if (!stringLetter) {
35+
return checkRemainingPatternsObjects(
36+
patternTransform,
37+
b,
38+
lastMatch
39+
);
40+
}
41+
42+
const checkResult = checkLetterMatch(stringLetter, patternObj);
43+
44+
if (checkResult) {
45+
if (a < s.length) a++;
46+
if (!patternObj.star) b++;
47+
lastMatch = {
48+
letter: stringLetter,
49+
star: patternObj.star,
50+
};
51+
} else {
52+
if (!patternObj.star) {
53+
cont = false;
54+
} else {
55+
b++;
56+
}
57+
}
58+
}
59+
60+
return a === s.length && b === patternTransform.length;
61+
};
62+
63+
function checkLetterMatch(stringLetter, patternObj) {
64+
if (!stringLetter) return false;
65+
if (stringLetter === patternObj.letter || patternObj.letter === ".")
66+
return true;
67+
68+
return 0;
69+
}
70+
71+
function transformPatternString(p) {
72+
const arrayOfLetters = Array.from(p);
73+
74+
const result = [];
75+
76+
for (let i = 0; i < arrayOfLetters.length; i++) {
77+
const letter = arrayOfLetters[i];
78+
let star = false;
79+
if (letter === "*") continue;
80+
81+
if (arrayOfLetters[i + 1] === "*") star = true;
82+
83+
result.push({
84+
letter,
85+
star,
86+
});
87+
}
88+
89+
return result;
90+
}
91+
92+
function checkRemainingPatternsObjects(patternTransform, b, lastMatch) {
93+
for (let i = b; i < patternTransform.length; i++) {
94+
if (patternTransform[i].star === false) {
95+
if (
96+
(lastMatch.letter === patternTransform[i].letter ||
97+
patternTransform[i].letter === ".") &&
98+
lastMatch.star
99+
) {
100+
continue;
101+
} else return false;
102+
}
103+
}
104+
105+
return true;
106+
}
107+
108+
function preliminaryCheck(s, noStars) {
109+
let a = 0;
110+
let b = 0;
111+
112+
while (true) {
113+
if (a === s.length || b === noStars.length) break;
114+
115+
if (s[a] === noStars[b].letter || noStars[b].letter === ".") {
116+
b++;
117+
}
118+
a++;
119+
}
120+
121+
return b === noStars.length;
122+
}
123+
124+
console.log(isMatch("aasdfasdfasdfasdfas", "aasdf.*asdf.*asdf.*asdf.*s"));

37-sudokuSolver.js

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* @param {character[][]} board
3+
* @return {void} Do not return anything, modify board in-place instead.
4+
*/
5+
var solveSudoku = function (board) {
6+
let allPossibilities = [...board];
7+
const scanRow = (i, j) => {
8+
for (let rowIndex = 0; rowIndex < 9; rowIndex++) {
9+
if (typeof allPossibilities[i][j] !== "string") {
10+
if (allPossibilities[i][j].length === 1) {
11+
allPossibilities[i][j] = allPossibilities[i][j][0];
12+
completed[convertIndeces(i, j)] = true;
13+
break;
14+
}
15+
16+
if (typeof allPossibilities[i][rowIndex] === "string") {
17+
const selected = allPossibilities[i][rowIndex];
18+
if (allPossibilities[i][j].includes(selected)) {
19+
const filteredPossibilites = allPossibilities[i][
20+
j
21+
].filter((possibility) => possibility !== selected);
22+
23+
allPossibilities[i][j] = filteredPossibilites;
24+
}
25+
}
26+
}
27+
}
28+
};
29+
30+
const scanColumn = (i, j) => {
31+
for (let colIndex = 0; colIndex < 9; colIndex++) {
32+
if (typeof allPossibilities[i][j] !== "string") {
33+
if (allPossibilities[i][j].length === 1) {
34+
allPossibilities[i][j] = allPossibilities[i][j][0];
35+
completed[convertIndeces(i, j)] = true;
36+
break;
37+
}
38+
39+
if (typeof allPossibilities[colIndex][j] === "string") {
40+
const selected = allPossibilities[colIndex][j];
41+
if (allPossibilities[i][j].includes(selected)) {
42+
const filteredPossibilites = allPossibilities[i][
43+
j
44+
].filter((possibility) => possibility !== selected);
45+
46+
allPossibilities[i][j] = filteredPossibilites;
47+
}
48+
}
49+
}
50+
}
51+
};
52+
53+
const scanSquare = (i, j) => {
54+
for (let squareIndex = 0; squareIndex < 9; squareIndex++) {
55+
if (typeof allPossibilities[i][j] !== "string") {
56+
if (allPossibilities[i][j].length === 1) {
57+
allPossibilities[i][j] = allPossibilities[i][j][0];
58+
completed[convertIndeces(i, j)] = true;
59+
break;
60+
}
61+
62+
if (
63+
typeof allPossibilities[mapToSquare(i, j, squareIndex).row][
64+
mapToSquare(i, j, squareIndex).column
65+
] === "string"
66+
) {
67+
const selected =
68+
allPossibilities[mapToSquare(i, j, squareIndex).row][
69+
mapToSquare(i, j, squareIndex).column
70+
];
71+
72+
if (allPossibilities[i][j].includes(selected)) {
73+
const filteredPossibilites = allPossibilities[i][
74+
j
75+
].filter((possibility) => possibility !== selected);
76+
77+
allPossibilities[i][j] = filteredPossibilites;
78+
}
79+
}
80+
}
81+
}
82+
};
83+
84+
const convertIndeces = (i, j) => {
85+
return i * 9 + j;
86+
};
87+
88+
const mapToSquare = (i, j, squareIndex) => {
89+
const initialRow = Math.floor(i / 3) * 3;
90+
const initialCol = Math.floor(j / 3) * 3;
91+
92+
return {
93+
row: initialRow + Math.floor(squareIndex / 3),
94+
column: initialCol + (squareIndex % 3),
95+
};
96+
};
97+
98+
let completed = Array(81).fill(false);
99+
100+
for (let i = 0; i < 9; i++) {
101+
for (let j = 0; j < 9; j++) {
102+
if (allPossibilities[i][j] === ".") {
103+
allPossibilities[i][j] = [
104+
"1",
105+
"2",
106+
"3",
107+
"4",
108+
"5",
109+
"6",
110+
"7",
111+
"8",
112+
"9",
113+
];
114+
} else {
115+
completed[convertIndeces(i, j)] = true;
116+
}
117+
}
118+
}
119+
120+
while (true) {
121+
for (let i = 0; i < 9; i++) {
122+
for (let j = 0; j < 9; j++) {
123+
scanRow(i, j);
124+
scanColumn(i, j);
125+
scanSquare(i, j);
126+
}
127+
}
128+
console.log(completed);
129+
130+
131+
if (completed.every((value) => value)) {
132+
break;
133+
}
134+
}
135+
for (let i = 0; i < 9; i++) {
136+
for (let j = 0; j < 9; j++) {
137+
board[i][j] = allPossibilities[i][j];
138+
}
139+
}
140+
141+
return board;
142+
};
143+
144+
const board = [
145+
[".", ".", "9", "7", "4", "8", ".", ".", "."],
146+
["7", ".", ".", ".", ".", ".", ".", ".", "."],
147+
[".", "2", ".", "1", ".", "9", ".", ".", "."],
148+
[".", ".", "7", ".", ".", ".", "2", "4", "."],
149+
[".", "6", "4", ".", "1", ".", "5", "9", "."],
150+
[".", "9", "8", ".", ".", ".", "3", ".", "."],
151+
[".", ".", ".", "8", ".", "3", ".", "2", "."],
152+
[".", ".", ".", ".", ".", ".", ".", ".", "6"],
153+
[".", ".", ".", "2", "7", "5", "9", ".", "."],
154+
];
155+
solveSudoku(board);

66-plusOne.js

Whitespace-only changes.

0 commit comments

Comments
 (0)