Skip to content
Advertisement

Given 2 strings, remove only one digit to make 1 string lexicographically smaller

I am trying to work through a coding problem of string manipulation in Java. The question is that

Given two strings S and T consisting of digits and lowercase letters, you are allowed to remove only one digit from either string, count how many ways of removal to make S lexicographically smaller than T.

I came up with this test case myself. If s = ‘3ab’ and t = ‘cd’, return 1. If s = ‘123ab’ and t = ‘423cd’, return 6.

My idea is to use 2 for loops and go through each string by checking if a char is digit, remove it and compare with the other string.

JavaScript

As you can see the space complexity is pretty bad, and the code also seems redundant. Is there a way to optimize this piece of code? Does anyone see a way to not use a string builder or create a new string each time? Any input is appreciated!

Advertisement

Answer

I did it using streams and compared it to your approach with random strings of length 10. I ran 1 million test cases of those strings and the two methods provided the same results.

The stream part is fairly straightforward. I use an IntStream to index into a string to build substrings based on digit location. Then I filter based on a passed BiFunction lambda that acts as a two argument predicate. Filtering on that I count the successes.

I do this twice, reversing the arguments and the predicate logic, and sum up the two counts.

JavaScript
Advertisement