diff --git a/modulo6/resolvendo-problemas/1.ts b/modulo6/resolvendo-problemas/1.ts new file mode 100644 index 0000000..f0c41f7 --- /dev/null +++ b/modulo6/resolvendo-problemas/1.ts @@ -0,0 +1,13 @@ +function isOneEdit(strA: string, strB: string): boolean { +if (Math.abs(strB.length - strA.length) > 1) return false + +if (strA.length > strB.length) return strA.includes(strB) +if (strB.length > strA.length) return strB.includes(strA) + +let charsDiffCount = 0 +for (let i = 0; i < strA.length; i++) { + if (strA[i] !== strB[i]) charsDiffCount++ +} + +return charsDiffCount === 1 +} \ No newline at end of file diff --git a/modulo6/resolvendo-problemas/2.ts b/modulo6/resolvendo-problemas/2.ts new file mode 100644 index 0000000..8e5af17 --- /dev/null +++ b/modulo6/resolvendo-problemas/2.ts @@ -0,0 +1,22 @@ +export const stringCompression = (input) => { + const substrings = []; + let lastChar = input[0]; + let charCount = 0; + + for (const char of input) { + if (char !== lastChar) { + substrings.push(lastChar + charCount); + lastChar = char; + charCount = 0; + } + charCount++; + } + + substrings.push(lastChar + charCount); + let result = ""; + for (const key of substrings) { + result += key; + } + + return result.length > input.length ? input : result; + }; \ No newline at end of file