The guarantee is going to help us a lot here, and the guarantee is that it is always possible to make a palindrome by removing only one character. Obviously the most obvious approach is to take out every single character and check for palindrome but that gives us around (10^6)^2 complexity which will not fit in 1 second time limit for sure. Instead we can try a little trickier method, it is guaranteed that we are sure going to be able to form a palindrome, so if initially the first character is not equal to the last character then we can conclude that either we have to remove the first character in order to form a palindrome or the last. If they are equal then we need to check the second character to be equal to the pre-last character and if it is not then the character which must be removed is either the second one or the pre last one , and so on.
This is the blog of site xoptutorials.com. All the news will be posted here and on the facebook page of xoptutorials.
Showing posts with label HackerRank::Algorithms::Strings. Show all posts
Showing posts with label HackerRank::Algorithms::Strings. Show all posts
Saturday, July 18, 2015
Friday, April 24, 2015
HackerRank::Algorithms::Strings::Bigger is Greater
There is a good function in "algortihm" library which handles the next permutation formation. Check the usage of that function below. It simply takes two arguments iterator first and the iterator last.
HackerRank::Algorithms::Strings::Two Strings
This task might look a little hard from the first look, but it will become extremely easy when you realize that only checking the existence of single letters form a to z is enough to answer the question ;)
HackerRank::Algorithms::Strings::Anagram
Like the previous task about anagrams, there is no difference in which one we will try to turn into another one, the difference of letter quantities is what matters. The rest is implementation based.
HackerRank::Algorithms::Strings::Make it Anagram
There is no difference whether we try to change the first to look like the second one, of to change the second one to look like the first one. The thing is that if one of them has for example X 'a' letters and the other one has Y 'a' letters, then no matter what we do we need to do abs(X-Y) deletions in order to match the number of 'a' s. We have to do the same for all the 26 letters, in other words the answer is the sum of differences of each letters quantities in the strings.
HackerRank::Algorithms::Strings::Gemstones
We simply need to check all the letters from a to z to exist in all of the given strings at the same time.
HackerRank::Algorithms::Strings::Game of Thrones - I
The only thing which we care about is the number of all the letters from a to z. If the string has an even length then we need to have even number of letters of each type so that we can form a palindrome. If the initial string has an odd length, then we need to have exactly one letter repeating odd number of times and the other letters repeating even number of times.
HackerRank::Algorithms::Strings::Alternating Characters
We need to have a look at a single deletion, if there are 2 equal adjacent letters then surely one of them has to be deleted and no matter which one we chose to delete, the remaining string is going to be the same, so by simply counting the number of equal adjacent letters and deleting them will give us the final result.
HackerRank::Algorithms::Strings::Pangrams
we can keep a boolean array MARK where MARK[I] is true if we have already met the Ith letter and false otherwise. Now, we loop over all the letters of the string, lowercase all of them, then check the MARK array, if it's false then we mark that letter as true and increase our counter of unique letters. in the end, if the counter equals to 26 then we have met all the letters, otherwise we didn't.
HackerRank::Algorithms::Strings::Funny String
One simple loop over the given string is enough to check the task requirement.
Subscribe to:
Posts (Atom)