Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Can't it be done in O(N) ?

Utkarsh Gupta

Aug 22, 2023

If we create the frequency map and then iterate through the values of the map. Each iteration the minimum number of characters required for the current key will be T1-1 where T1 is the count of characters of current key. Now for frequency of next character will be reduced from T2 to T2-(T1-1). T2` = T2-(T1-1)-1 will be the remaining frequency of next character so we can repeat the same procedure and at the end if we are left with 0,1 or 2 then it is possible to create a string with all the characters such that no two same characters are adjacent.

Could this be a possible solution?

0

0

Comments
Comments
U
Utkarsh Gupta2 years ago

Hmm....On a second thought it can't be...because we have to build and return the string.