Skip to content

Not handled second part of the question #50

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions chapter01/1.6 - String Compression/strComp.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
var strComp = function(string) {
var compressed = '';
var currChar = '';
var currCount = '';
var maxCount = 1;
for (var i = 0; i < string.length; i++) {
if (currChar !== string[i]) {
console.log(currChar, string[i], i);
compressed = compressed + currChar + currCount;
maxCount = Math.max(maxCount, currCount);
currChar = string[i];
currCount = 1;
const stringCompression = (string) => {
let compressed = '';
let currentCharacter = '';
let currentCount = '';
let maxCount = 1;
for(var i = 0; i < string.length; i++) {
if(currentCharacter !== string[i]) {
compressed = compressed + currentCharacter + currentCount;
maxCount = Math.max(maxCount, currentCount);
currentCharacter = string[i];
currentCount = 1;
} else {
currCount++;
currentCount++;
}
}
compressed = compressed + currChar + currCount;
maxCount = Math.max(maxCount, currCount);


compressed = compressed + currentCharacter + currentCount;
maxCount = Math.max(maxCount, currentCount);

if(string.length < compressed.length) {
return string;
}

return maxCount === 1 ? string : compressed;
};
}

// Test
console.log('aaaaaa', strComp('aaaaaa'), 'a6');
console.log('aabcccccaaa', strComp('aabcccccaaa'), 'a2b1c5a3');
console.log('aaaaaa', stringCompression('aabcdef')); // aabcdef
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a2bcdef for this one? I'm thinking that the only stuff you should not compress is when the rep is 1, but for anything more than that, will be good to compress -- for simplicity sake. Though I understand |a2| == |aa|

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the late response.

per my understanding of the question, if the length of the output is greater then or equal to the length of input we should simply return the input.

console.log('aaaaaa', stringCompression('aabbcc')); // a2b2c2