parent
f16f797381
commit
35c77a3a23
@ -0,0 +1,3 @@
|
|||||||
|
import { isPalindrome } from "./isPalindrome";
|
||||||
|
|
||||||
|
export default isPalindrome;
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { isPalindrome } from "./isPalindrome.ts";
|
||||||
|
|
||||||
|
describe("isPalindrome", () => {
|
||||||
|
it("A man, a plan, a canal: Panama => true", () => {
|
||||||
|
expect(isPalindrome("A man, a plan, a canal: Panama")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("race a car => false", () => {
|
||||||
|
expect(isPalindrome("race a car")).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(" _ => true", () => {
|
||||||
|
expect(isPalindrome(" ")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(" 0P => false", () => {
|
||||||
|
expect(isPalindrome("0P")).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
function isLetter(c: string) {
|
||||||
|
if (/\d/.test(c)) return true;
|
||||||
|
return c.toLowerCase() != c.toUpperCase();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isPalindrome(s: string): boolean {
|
||||||
|
let L = 0;
|
||||||
|
let R = s.length - 1;
|
||||||
|
|
||||||
|
while (L <= R) {
|
||||||
|
let lChar = s[L].toLowerCase();
|
||||||
|
let rChar = s[R].toLowerCase();
|
||||||
|
|
||||||
|
if (!isLetter(lChar)) {
|
||||||
|
L++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!isLetter(rChar)) {
|
||||||
|
R--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lChar !== rChar) return false;
|
||||||
|
L++;
|
||||||
|
R--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue