Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Dollar Sign Deletion (Main Thread)

Here is the interview question prompt, presented for reference.

You're given an array of strings containing alphabetical characters and certain $ characters. A $ represents a DELETE action whereby the character before it is deleted.

Each of these strings will be run through a method to operate on the $ DELETE action. We want to find out if the final string is the same for all of them. Let's take an example:

String[] input = {"f$st", "st"};
isDollarDeleteEqual(input);
// true
// true because both become "st"
let input = ["f$st", "st"];
isDollarDeleteEqual(input);
// true
// true because both become "st"
input = ["f$st", "st"]
is_dollar_delete_equal(input)
# True
# True because both become 'st'
std::vector<std::string> input = {"f$st", "st"};
isDollarDeleteEqual(input);
// true
// true because both become "st"
string[] input = {"f$st", "st"};
IsDollarDeleteEqual(input);
// true
// true because both become "st"
input := []string{"f$st", "st"}
isDollarDeleteEqual(input)
// true
// true because both become "st"

Can you find a solution in O(n) time and constant space?

Constraints

  • The input arrays can be of any size
  • Every string has at least a single character
  • The string will consist of dollar signs and lowercase alphabets
  • Expected overall time complexity : O(n)
  • Expected space complexity : O(n) for good solution, O(1) for asymptotically optimal solution

You can see the full challenge with visuals at this link.

Challenges • Asked about 5 years ago by Hardik

Jake from AlgoDaily Commented on Sep 04, 2019:

This is the main discussion thread generated for Dollar Sign Deletion.

Hardik Commented on Sep 04, 2019:

Test case 1 and 2 are both same yet it is shown that their results are different.

lazycoder Commented on Mar 11, 2020:

['f$ec', 'ec'] should return true

['f$ec', 'ec'] should return false
These 2 test cases are contradictory.

Rahat Zaman Commented on Feb 05, 2021:

Yes, we have now fixed this problem.

yorkcook13 Commented on Mar 24, 2021:

I can't figure out why my code doesn't work. It works when I try it in repl.it, is it because it doesn't make the time constraint?? Thanks!

function isDollarDeleteEqual(arr) {
let mapped = arr.map(item => getFinalStr(item));
return mapped.every(item => item === mapped[0]);
}
function getFinalStr(str) {
let arr = str.split('');

for(i =0; i < arr.length; i++) {
if(arr[i] === '$') {
arr.splice(i-1, 2);

}
}
return arr.join('');
}

Yuri Commented on Jan 07, 2022:

Just to make sure this thread is finished with an answer and not a question.
This code fails tests 3 and 4 because getFinalStr() modifies the array arr inside the for cycle. After splice finishes its job, i references the position where "$" was before the deletion of 2 characters. Therefore, continuing iteration effectively skips the next two characters. To make this code work you could add i -= 2 after calling the splice method. Alternatively, you should consider using other ways like while loop granting you more control over the iteration variable.