educative.io

Educative

Doubt regarding the input constraint

Is there any constraint on the input for using only lowercase characters for inputted string and pattern? If there is, we can use arrays of 26 characters to keep track of the frequency of both the inputted string and the pattern.


Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/4679331609575424

Hi @Shantanu_Singh. There isn’t any constraint on the input for using only lowercase characters for inputted string and pattern. But even if there was, using hashmaps instead of 2 arrays of 26 size would be more efficient.

Example:

Input String: aabbbcde
Pattern String: aab

Comparison:

Here in this example, we only need five indexes to keep track of the characters’ frequencies present in the input string. If we use hashmap, then we will only make 5 indexes, but if we use an array of size 26 then 26-5=21 extra indexes will be made, which will be of no use. Similarly, we only need 2 indexes to keep track of the character’s frequencies present in the pattern string. If we use hashmap, then we will only make 2 indexes, but if we use an array of size 26 then 26-2=24 extra indexes will be made, which will be of no use.

Happy Learning :smiley: