-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chapter01/1.3 - URLify/urlify.js #20
Comments
You could also use Regex. But the exercise could also be to see how you would build this out in a more general way. I assumed that this was simple for the reason to see how would would modularize your code and go with a faster runtime, rather than to focus on how you would manipulate strings in a language that didn’t have these tools. const charMap = {
‘ ‘: ‘%20’
};
function replaceCharacter(str, targetChar, replacementChar) {
const charRegex = new RegExp(targetChar, ‘gi’);
return str.replace(charRegex, replacementChar);
}
function urlify(url) {
let modifiedURL = url.trim();
for(let charToReplace in charMap) {
const replacementChar = charMap[charToReplace];
modifiedURL = replaceCharacter(modifiedURL, charToReplace, replacementChar);
}
return modifiedURL;
} Of course, if we are pretending there are no language features that trivialize the problem or we need to account for space (maybe the target device is embedded with insanely small resources), then we could do something along the lines of the existing code or the solution from the book. Basically it seems your interviewer can make it as hard as they want to, so we should probably consider all cases. |
Apparently there is an issue with this solution because when you run it the result it returns is missing some characters: @skawaguchi @jarkin13 The thing that I don't like about using specific javascript methods/properties, specially if you can do without them, is that if your interviewer asks you about the Big O for your solution, unless you know what's happening behind curtains, you won't be able to provide an answer. |
I came here to write, you could simply use regex, I do like your solution @jarkin13, I did not think to use
|
I think the following is a general solution and that which is more elegant.
I've submitted a PR for the same. |
For this problem, why wouldn't you do something along the lines of:
I know it says that it provides with the true length of the string, but would something a long these lines not be acceptable?
The text was updated successfully, but these errors were encountered: