Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // r/ihadatime
- //
- //
- //
- // Sorry for my English xd
- //
- //
- // Made by @ncon
- //
- // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- // WARNING: Letters are probably case sensetive
- // (depends on how javascript operator == works on characters)
- // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- //
- // HOWTO: How to solve this kind of problems
- // 1. Try to understand the problem
- // 2. DON'T start from code, never, you will get the same thing as this guy did, analyze the problem
- // 3. Find how this can be solved, figure out algorithm, you can try
- // to play with this on paper, for example:
- // ABCADA
- // CBACD
- //
- // Iteration 1:
- // --result is ""--
- // Remove:
- // ABCADA
- // CBACD
- // Equal ? No;
- // Closest indexes:
- // [0, 2]
- // Remove:
- // BCADA
- // CD
- //
- // Iteration 2:
- // --result is "A"--
- // Remove:
- // BCADA
- // CD
- // Equal ? No;
- // Closest indexes:
- // [1, 0]
- // Remove:
- // ADA
- // D
- //
- // Iteration 3:
- // --result is "AC"--
- // Remove:
- // D
- // D
- // Equal ? Yes - add & return;
- //
- // The result is: ACD
- //
- //
- //
- // MY APPROACH:
- //
- //
- // While string aren't empty: <------ (repeat) -------------------.
- // \
- // Filter unmatching characters |
- // If strings are equal, push one of them to result and return it |
- // Find closest letter indexes |
- // Check if closest array (in this case, tuple) isn't empty, |
- // otherwise javascript will throw exception /
- // Remove characters behind & on indexes then add it into result /
- //---------------------------------------------------------------'
- //Filter characters that doesn't match in strings
- function filterUnmatching(s1, s2)
- {
- var result = ["", ""];
- s1.split('').forEach(function(item){
- // Check if items from first string is in the second.
- if (s2.indexOf(item) != -1) result[0] += item;
- });
- // Removed to optimize, result: -1ms/100reqests;
- // REMOVED: s2.split('').forEach(function(item){
- // REMOVED: if (s1.indexOf(item) != -1) result[1] += item;
- // REMOVED: });
- result[1] = s2;
- return result;
- }
- function closestIndex(s1, s2) {
- var idxs = {};
- s1.split('').forEach(function(item, i){
- // Figure out what letter from first string is closest to second
- var iof = s2.indexOf(item);
- if (iof != -1)
- {
- idxs[i + iof] = [i, iof, item];
- }
- });
- s2.split('').forEach(function(item, i){
- // Figure out what letter from second string is closest to first
- var iof = s1.indexOf(item);
- if (iof != -1)
- {
- // `i + iof` is the price of the index, if it's less, it's better ;)
- idxs[i + iof] = [iof, i, item];
- }
- });
- for (var key in idxs)
- {
- // Since objects sort automatically
- // It returns us the 'lowest price index' (see mention above)
- // I didn't know any other solution because provider turned my internet off, lol
- return idxs[key];
- }
- }
- function removeBehind(indexes, s1, s2) {
- // Removes all characters behind + the current character (indexed)
- return [s1.substr(indexes[0]+1), s2.substr(indexes[1]+1)];
- }
- //The main function
- function subseq(s1, s2)
- {
- var result = "";
- while (s1 != "" || s2 != "")
- {
- // The main code that assembles everything above, see 'MY APPROACH' section above for more info
- var filtered = filterUnmatching(s1, s2);
- s1 = filtered[0];
- s2 = filtered[1];
- if (s1 == s2)
- {
- result += s1;
- return result;
- }
- var closest = closestIndex(s1, s2);
- if (closest == null) return result;
- result += closest[2];
- var removed = removeBehind(closest, s1, s2);
- s1 = removed[0];
- s2 = removed[1];
- }
- return result;
- }
- // Generates rand string for test and benchmark
- function randstr() {
- var res = "";
- var chars = "UIOPASDFCV"; // Here you can put your own characters
- for (var i = 0; i < 7; i++)
- {
- res += chars[parseInt(Math.random()*chars.length)]
- }
- return res;
- }
- // Get UNIX timestamp now (in milliseconds)
- var a = Date.now()
- var iters = 10; // Here you can set your arbitrary value of iterations
- for (var i = 0; i < iters; i++)
- {
- var s1 = randstr();
- var s2 = randstr();
- console.log(s1+", "+s2+": "+subseq(s1, s2));
- }
- // Get UNIX timestamp now again (in milliseconds) and
- // Subtract from previous time, thus, we get how much
- // Did this operation took time.
- console.log(Date.now() - a+"ms")
- //
- // My personal benchmarks btw (without console.log, on nodejs):
- //
- // 1 iteration:
- // ~ 1ms
- // 10 iterations:
- // ~ 0ms (it's faster for some reason lmao)
- // 100 iterations:
- // ~ 1ms
- // 1000 iterations:
- // ~ 14ms
- // 10000 iterations:
- // ~ 69ms (oh yeah)
- // 100000 iterations:
- // ~ 578ms
- // 1000000 iterations:
- // ~ 5522ms
- // 10000000 iterations:
- // ~ 59483ms
- // 100000000 iterations:
- // ~ I have to go sleep rn, will add tomorrow
- //
- //
- //
- // Made by @ncon and his 2 hours of commenting
- // Bye
- //
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement