Created
January 23, 2026 15:36
-
-
Save felisio/501128c5c705dc73d36fc449cf2002d4 to your computer and use it in GitHub Desktop.
leetCode examples
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
| function reversString(str: string) { | |
| return [...str].reverse().join(""); | |
| } | |
| function verifyPalindrome(str: string) { | |
| return str === reversString(str); | |
| } | |
| function longestPalindrome(s: string): string { | |
| const strSize = s.length; | |
| const result = new Set(); | |
| for (let gap = 0; gap <= strSize; gap++) { | |
| for (let i = 0, j = gap; j < strSize; i++, j++) { | |
| result.add(s.substring(i, j + 1)); | |
| } | |
| } | |
| const palindromeList = [...result].filter((str) => | |
| verifyPalindrome(str as string), | |
| ); | |
| const longestPalindrome = palindromeList.reduce((prev: any, curr: any) => { | |
| if (curr.length > prev.length) { | |
| return curr; | |
| } | |
| return prev; | |
| }, ""); | |
| return longestPalindrome as string; | |
| } | |
| /* console.log(longestPalindrome("jglknendplocymmvwtoxvebkekzfdhykknufqdkntnqvgfbahsljkobhbxkvyictzkqjqydczuxjkgecdyhixdttxfqmgksrkyvopwprsgoszftuhawflzjyuyrujrxluhzjvbflxgcovilthvuihzttzithnsqbdxtafxrfrblulsakrahulwthhbjcslceewxfxtavljpimaqqlcbrdgtgjryjytgxljxtravwdlnrrauxplempnbfeusgtqzjtzshwieutxdytlrrqvyemlyzolhbkzhyfyttevqnfvmpqjngcnazmaagwihxrhmcibyfkccyrqwnzlzqeuenhwlzhbxqxerfifzncimwqsfatudjihtumrtjtggzleovihifxufvwqeimbxvzlxwcsknksogsbwwdlwulnetdysvsfkonggeedtshxqkgbhoscjgpiel")); */ | |
| //console.log(longestPalindrome("babad")); | |
| function longestCommonPrefix(strs: string[]): string { | |
| if (!strs.length) { | |
| return ""; | |
| } | |
| let prefix = strs[0]; | |
| for (let i = 1; i < strs.length; i++) { | |
| while (!strs[i].startsWith(prefix)) { | |
| prefix = prefix.slice(0, -1); | |
| if (!prefix) return ""; | |
| } | |
| } | |
| return prefix; | |
| } | |
| //console.log("TESTE 1", longestCommonPrefix(["flower", "flow", "flight"])); | |
| //console.log("TESTE 2", longestCommonPrefix(["catarbuna", "catarina"])); | |
| //console.log("TESTE 3", longestCommonPrefix(["cat", "dog", "catarina"])); | |
| function findElement(list: number[], target: number) { | |
| const set = new Set(list); | |
| return set.has(target); | |
| } | |
| console.log("TESTE 1", findElement([6, 2, 1, 7], 2)); | |
| console.log("TESTE 2", findElement([6, 5, 1, 7], 2)); | |
| function findSmallestElement(list: number[]) { | |
| const set = new Set(list); | |
| let i = 1; | |
| while (set.has(i)) i++; | |
| return i; | |
| } | |
| console.log("TESTE 1", findSmallestElement([6, 2, 1, 7])); // 3 | |
| console.log("TESTE 2", findSmallestElement([6, 2, 1, 3, 5, 7])); // 4 | |
| function findBiggestElements(list: number[], k: number) { | |
| return list.sort((a, b) => b - a).slice(0, k); | |
| } | |
| console.log("TESTE 1", findBiggestElements([6, 2, 1, 7], 3)); // 3 | |
| console.log("TESTE 2", findBiggestElements([6, 2, 1, 3, 5, 7], 4)); // 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment