Created
February 27, 2025 20:47
-
-
Save theWhiteFox/4d713fee22f57ba893199eec585552ba to your computer and use it in GitHub Desktop.
brute force solution for encode decode neet code problem
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 encode(strs): | |
encodedStr = "" | |
// Iterate thru strings | |
for each str of strs: | |
// Escape colons in the string | |
escapedStr = str.replace(/:/g, "::") | |
// Append the length of the escaped string to the encodedStr | |
encodedStr += length(escapedStr) + ":" + escapedStr | |
// Return the encoded string | |
return encodedStr | |
function decode(encodedStr): | |
// List to store decoded strings | |
decodedStrs = [] | |
// Initialize index | |
idx = 0 | |
// Loop thru the encoded string | |
while idx < length(encodedStr): | |
// Find the index of the delimiter | |
delimiterIdx = encodedStr.indexOf(":", idx) | |
// Get the length of the string | |
strLength = parseInt(encodedStr substring from idx to delimiterIdx) | |
// Move the index after the delimiter | |
idx = delimiterIdx + 1 | |
// Extract the escaped string using the length | |
escapedStr = encodedStr.substring(idx, idx + strLength) | |
// Replace double colons with single colons | |
decodedStr = escapedStr.replace(/::/g, ":") | |
// Add the decoded string to the list | |
append decodedStr to decodedStrs | |
// Move the index forward by the strLength | |
idx += strLength | |
// Return decode strings | |
return decodedStrs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment