Skip to content

Comments

Implement lexGreaterPermutation function#6

Merged
Subhosjx merged 1 commit intoSubhosjx:mainfrom
SjxSubham:main
Oct 27, 2025
Merged

Implement lexGreaterPermutation function#6
Subhosjx merged 1 commit intoSubhosjx:mainfrom
SjxSubham:main

Conversation

@SjxSubham
Copy link
Contributor

Intuition

Size of string is in [1,300]. Approach must be O(n^2) or O(n^2 logn).
When can a string be greater than another string of the same length?
if and only if for all characters at j in [0,i] s[j] = t[j]
and s[i+1]>t[i+1]
where i +1 < n (for proper indexing)

Approach

  1. Create a frequency map of string s, initialise ans as empty string
  2. Run a loop from i=0 to i =n-1
    a. Try to match the first ith characters of s and target (0 to i-1 in 0 indexing)
    b. for the ith (0 indexed) character of target, find the first character greater than it in the remaining frequency map
    c. if no such character found continue
    d. else for all indices from i +1 onwards till n-1, put all remaining chars of map in ascending order.
    e. if ans is empty string, set ans as found string, else set ans as minimum between ans and found string
  3. return ans

@Subhosjx Subhosjx added the hacktoberfest-accepted hacktoberfest-accepted label Oct 27, 2025
@Subhosjx Subhosjx merged commit 93d117d into Subhosjx:main Oct 27, 2025
1 check passed
@github-actions
Copy link

🎉 Congrats on getting your PR merged in, @SjxSubham! 🙌🏼

Thanks for your contribution every effort helps improve the project.

Looking forward to seeing more from you! 🥳✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberfest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants