From 84484dbf0eb03e76e5119c0292655a7a9755a582 Mon Sep 17 00:00:00 2001 From: waikhuen Date: Fri, 9 Dec 2022 14:05:15 +0800 Subject: [PATCH] 1016 binary string solution --- TypeScript/[1016]binary-string.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 TypeScript/[1016]binary-string.ts diff --git a/TypeScript/[1016]binary-string.ts b/TypeScript/[1016]binary-string.ts new file mode 100644 index 0000000000..3234419cf1 --- /dev/null +++ b/TypeScript/[1016]binary-string.ts @@ -0,0 +1,30 @@ +function queryString(s: string, n: number): boolean { + const reviewedNumber = new Set(); + let current = n; + while(current > 0) { + if (!reviewedNumber.has(current)) { + if (!s.includes(Number(current).toString(2))) { + return false; + } + + addMultiplesToSet(reviewedNumber, current); + } + + current--; + } + return true; +}; + +function addMultiplesToSet(set: Set, n: number): void { + console.log(`Given: ${n}`); + let num = n; + do { + console.log(`Adding number ${num}`); + set.add(num); + num = num >> 1; + + if (num === 0) { + break; + } + } while(true); +}