Comments (4)
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.
from ctci-6th-edition-javascript.
Apparently there is an issue with this solution because when you run it the result it returns is missing some characters: Mr%20hohn%20ith%20
.
Also without using any JS specific methods/properties you could still simplify this even more.
@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.
from ctci-6th-edition-javascript.
I came here to write, you could simply use regex, I do like your solution @jarkin13, I did not think to use trim();
const URLify = (string) => string.replace(/[ \t]+$/g, '').replace(/[ ]/g, '%20');
from ctci-6th-edition-javascript.
I think the following is a general solution and that which is more elegant.
var urlify = function(str, length) {
var strArr = [];
for(var i = 0; i < length; i++) {
strArr[i] = str[i] == ' ' ? '%20' : str[i];
}
return strArr.join('');
};
I've submitted a PR for the same.
from ctci-6th-edition-javascript.
Related Issues (14)
- Pushing different solutions
- Add new contributor & remove me HOT 3
- 1.6 Length bug HOT 5
- My variant of the Urlify problem.
- Update ch1-q1? HOT 1
- maintenance: add linter as part of the CI HOT 1
- fix variable name for 3.3-stackOfPlates.js
- chore: add CONTRIBUTING.md file
- Mismatch in question 2.8 (loop detection in linked list)
- Test
- Test with CI HOT 2
- 8.12 N Queens has solution to a different problem HOT 1
- 1.6 String Comparison Error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ctci-6th-edition-javascript.