Posts

Showing posts from May, 2020

Rocks and Diamonds

Image
In this post I want to tell about one simple programming task with solving in Java. Suppose we have two strings with latin symbols out of order. First string may have duplicates and that string we call Rocks. Second string have no duplicates and we call it Diamonds. Our task to find out how many Diamonds exist in Rocks. Every symbol in second string is diamond. If we want to use brute forse it will be not very effective. In that case we need to iterate full Rocks as many times as Diamonds exist in second string and we need to iterate the whole Rocks - from beginning to end. To become more efficient we will to execute two preparations - sort Rocks and Diamonds alphabetically. Now we have two sorted strings. Let begin iterate throught Diamonds and for every Diamond we iterate Rocks, but not the whole - when we found Diamond in Rock we increment our result counter, then take next Rock we check if it more then Diamond alphabetically. If it is then we take next Diamond because previous ...