Rearrange string to an anagram of the string - get new position, old position values I'm writing a function to take an anagram pair (words or phrases spelled with the same letters) and determine how you could move the letters to change one into the other.

For example: Admirer -> Married

The results would be something like this:

[[0, 1], [1, 6], [2, 0], [3, 4], [4, 2], [5, 5], [6, 3]]


The character in the 0 (a) position moves to 1, the character in position 1 (d) moves to 6, etc. I've already successfully solved this but perhaps there's a better way!

Language of choice (required!) is javascript. Here's my current, working function:

function _get_new_positions(source, target) {
    // convert both strings to arrays - uppercase so we can find the destination
    // spot without worrying about cases
    source = source.toUpperCase().split('');
    target = target.toUpperCase().split('');

    var transform = [];
    var repeated = [];
    var found = 0;

    for(var i=0; i&lt;source.length; i++) {
        // keep track of how many times we've found and 'moved' this letter
        repeated[source[i]] = repeated[source[i]] ? repeated[source[i]] + 1 : 1;
        found = 0;
        want  = repeated[source[i]];
        for(var j=0; j&lt;target.length; j++) {
            if( target[j] == source[i] ) {
                found++;
                console.log('found is ' + found);
                if( found == want ) {
                    transform.push([i,j]);
                    break;
                }
            }
        }
    }
    return transform;
}


Thank you for your input.
You can simplify the logic by setting each character you find in target to NULL. I also like to use a "while" loop as shown below.

function _get_new_positions(source, target) {
    var transform = [], i, j, found, want;
// convert both strings to arrays - uppercase so we can find the destination without worrying about cases
    source = source.toUpperCase().split('');
    target = target.toUpperCase().split('');
    for(i=0; i&lt;source.length; i++) {
        j = 0;
        found = false;
        want  = source[i];
        while((j &lt; target.length) &amp;&amp; !(found = (target[j] == want))){j++}
        if (! found) {
            console.log("Cannot find character: " + want + " at [i]: " + i);
        } else {
            console.log("Found character: " + want + " at [i,j]: " + i + j);
            target[j] = ''; //blank out the character for future searches
            transform.push([i,j])
        }
    }
    return transform;
}