Please read the new rule regarding the restriction on the use of AI tools. ×

gauravit's blog

By gauravit, history, 3 years ago, In English

Given a string S containing lowercase English alphabet characters. The task is to calculate the number of distinct strings that can be obtained after performing exactly one swap.

In one swap, Geek can pick two distinct indexes i and j (i.e 1 < i < j < |S| ) of the string, then swap the characters at the position i and j.

S = "geek" Output: 6 Explanation: After one swap, there are only 6 distinct strings possible. (i.e "egek","eegk","geek","geke","gkee" and "keeg")`

I have made the solution using 2 for loops and a HashSet. I am thinking that is there any more optimized way to solve this problem?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it