Algorithmic Problem

Revision en1, by gauravit, 2021-07-19 10:51:28

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?

Tags #algorithms, #string, #optimization

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English gauravit 2021-07-19 10:51:28 636 Initial revision (published)