01. Odd Even Problem
The problem can be found at the following link: Question Link
My Approach
Initialize a vector
c
of size 26 to count the frequency of each character in the strings
.Traverse through the string
s
and update the count of each character in the vectorc
.Initialize a counter
cnt
to zero to keep track of characters that meet the odd/even criteria.Iterate through the vector
c
, checking if the frequency of each character is odd or even based on the character's position in the alphabet (i.e., position % 2).Increment the counter
cnt
for each character that meets the condition where the frequency's odd/even status matches the character's position % 2.Return "ODD" if
cnt
is odd; otherwise, return "EVEN".
Time and Auxiliary Space Complexity
Time Complexity:
(O(n))
, where( n )
is the length of the strings
, since we are traversing the string and then iterating through a fixed-size array of length 26.Auxiliary Space Complexity:
( O(1))
, since the vectorc
has a fixed size of 26 regardless of the input size.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated