We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
O(C(n, 2)) is actually O(n²) not O(n!) because C(n, 2) = n * (n-1) / 2
This gives an overall runtime complexity of O(mn²) for the approach you described (alternating sequence test for each pair of letters), where m is the length of the input string and n the size of the alphabet.
To answer you question: you can bring it down to O(mn) at the cost of O(n²) extra space. Some ways to do it are described here in the comment section.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
O(C(n, 2)) is actually O(n²) not O(n!) because C(n, 2) = n * (n-1) / 2
This gives an overall runtime complexity of O(mn²) for the approach you described (alternating sequence test for each pair of letters), where m is the length of the input string and n the size of the alphabet.
To answer you question: you can bring it down to O(mn) at the cost of O(n²) extra space. Some ways to do it are described here in the comment section.