Skip to content

yankaifyyy/min-repr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

min-repr.js

Minimal representation

Compute the start index of the minimal representation(also called Lexicographically minimal string rotation) of a string. This is an implementation of the Booth's O(n) algorithm(Lexicographically least circular substrings)

Usage

The minRepr function supports four kinds of parameters:

  • Plain string
  • Plain string with comparer
  • Array (includes comparable elements)
  • Array with comparer

The input parameter comparer is a comparison function, where comparer(a, b) === 0 means equal relationship, any number <0 means less than, and >0 means larger than.

Example

var a = 'acabc',
    b = [5, 0, 9, 7, 0];

minRepr(a);                     // 2
minRepr(a, (x, y) => {
    if (x > y) {
        return -1;
    } else if (x < y) {
        return 1;
    } else {
        return 0;
    }
});                             // 4
minRepr(b);                     // 4
minRepr(b, (x, y) => y - x);    // 2

About

Minimal representation of string

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published