Codesignal Challenge - fileNaming

Search for a command to run...

No comments yet. Be the first to comment.
Leetcode Problem Question https://leetcode.com/problems/valid-parentheses/ Updated On: Sept 15, 2023 Main things to note Object We're using Object to save the opening and closing of the parentheses Stack As you know stack works in a LIFO (Last In...
We’ve tried out lots of apps over time, and honestly—not all of them do what we really need. That’s why I’m writing this blog post: to share some of the challenges we’ve noticed with the Shopify Forms app and hear what others think too. If you’ve fou...
Hey everyone, Dorothi Viki here. For the last decade, I’ve been in the digital marketing trenches. I’ve managed budgets big and small, and I’ve seen firsthand how crucial it is to know what people are saying about you online. When you’re starting out...
If you’re a small business owner, freelancer, or consultant, your website is your storefront. But traditional website development can be expensive, slow, and intimidating. Platforms like SpreadSimple remove these barriers by allowing you to turn Goog...

For SEO professionals, content marketers, freelancers, and consultants, keyword research is non-negotiable. It’s the foundation of any effective search strategy. However, with so many tools behind paywalls, finding a reliable, high-quality, free Goog...
With the rise of AI agents and Claude-specific development environments, understanding Claude Code has become essential for modern developers, freelancers, and consultants. Whether you're building Multi-Component Programs (MCPs) or designing autonomo...
Note: Please note that experimental code ahead. Only tested in few test cases.
You are given an array of desired filenames in the order of their creation. Since two files cannot have equal names, the one which comes later will have an addition to its name in a form of (k), where k is the smallest positive integer such that the obtained name is not used yet.
Return an array of names that will be given to the files.
Example
For names = ["doc", "doc", "image", "doc(1)", "doc"], the output should be fileNaming(names) = ["doc", "doc(1)", "image", "doc(1)(1)", "doc(2)"].
function fileNaming(names) {
let newArr = [];
let objVals = {};
let genName = (name, size) => name + '(' + size + ')';
for (const name of names) {
let size = objVals[name] || 0;
objVals[name] = size + 1;
if (!size) {
newArr.push(name);
continue;
}
let newName = genName(name, size);
while(newArr.includes(newName)) {
size = size + 1;
newName = genName(name, size);
}
newArr.push(newName);
objVals[newName] = size;
}
return newArr;
}
// const names = ["doc", "doc","image", "doc(1)", "doc","doc(1)"];
const names = [ 'a(1)', 'a(6)', 'a', 'a', 'a', 'a', 'a', 'a','a','a','a','a',];
console.log(fileNaming(names));
Only have two test cases and got it working for them.
