Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6735319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:59:42+00:00 2026-05-26T10:59:42+00:00

Possible Duplicate: Counting the swaps required to convert one permutation into another I’m looking

  • 0

Possible Duplicate:
Counting the swaps required to convert one permutation into another

I’m looking for an algorithm that would count some kind of string distance where only allowed operation is transposition of two adjacent characters. For example:
string1: “mother”
string2: “moterh”
distance: 2 (first swap “h” with “e” and get “motehr” and then “h” with “r” resulting in “moterh”)
I know that Damerau–Levenshtein distance is quite alike that problem however it requires a lot of memory (I’d like it to work quite fast on words up to 1 000 000 characters). I’ve already written this:

int amo = 0;

for (int i = 0; i < n; i++)
{
    if (fromString[i] == toString[i])
        continue;
    char toWhat = toString[i];
    int where = -1;
    for (int j = i; j < n; j++)
    {
        if (fromString[j] == toWhat)
        {
            where = j;
            break;
        }
    }
    while (where != i)
    {
        char temp = fromString[where];
        fromString[where] = fromString[where - 1];
        fromString[where - 1] = temp;
        where--;
        amo++;
    }
}
cout << amo << endl;`

Strings are represented as char[n] where n is their length. I’m quite sure there’s a way to do it faster and I’d be very thankful if somebody will tell me how to do it or write some source code (best would be Java/Python/C++ but anything’s be great).

P.S. Excuse me any language mistakes, I’m not English and I haven’t mastered that language yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:59:43+00:00Added an answer on May 26, 2026 at 10:59 am

    Basically you’re asking for the edit distance algorithm, but only allowing the transposition (a.k.a. swapping, twiddling) operation. In the book “Introduction to Algorithms” you’ll find clues for implementing the twiddle operation, it’s one of the problems at the end of the chapter on dynamic programming. Also, in the book “The Algorithm Design Manual”, in the chapter on dynamic programming, there’s a complete implementation of the edit distance algorithm in C – sans the transposition operation (again, it’s one of the proposed exercises at the end of the chapter).

    In the above link, you’ll find that the typical way to implement the edit distance algorithm is by using dynamic programming, which has a cost of O(mn) time and O(mn) space. As far as I know, there’s no way to do it faster (e.g. in less than O(mn) time), but surely you can do it in less space – being smart, you can reduce the space to O(m), given that only the current row and the two previous rows in the table are needed for calculating the cost of a transposition operation.

    That is, assuming you only need the edit distance . If you need the actual edit operations, you’re stuck using O(mn) space for reconstructing the solution if using dynamic programming. However, you can reduce the space to O(min{m,n}) and reconstruct the actual edit operations, by using Hirschberg’s algorithm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Counting inversions in an array This is a programming question that I
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How to copy part of an array to another array in C#
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
Possible Duplicate: C++ convert int and string to char* Hello, i am making a
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but
Possible Duplicate: Counting unique items in data frame Hi my data includes same items
Possible Duplicate: How does the new automatic reference counting mechanism work? Can someone explain
Possible Duplicate: Counting Line Numbers in Eclipse How Can I count the number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.