LeetCode-1370 Increasing Decreasing String

1. Description

Given a string s, press ascending order of extraction character string added to the res, then extract characters in descending order to the end res, until all the characters in the original string extraction is completed.

2. Solution

The key is to extract each character again scanning frequency, then add back forward, one has to add character, we must promptly cnt minus one.

3. Code 

sortString String (S String, String RES = "") { 
    int CNT [26 is] = {}; 
    for (CH Auto: S) 
        ++ CNT [CH - 'A']; // read each letter frequency 
    while ( ! s.size () = res.size () ) {// add ascending, descending added 
        for (Auto I = 0; I <26 is; I ++) 
            RES = + String (- CNT [ I]> = 0. 1: 0, 'A' + I);? 
        for (int I = 25; I> = 0; Inc. (www.i-levelmedia.com)) 
            RES = + String (- CNT [I]> = 0. 1:? 0, 'A' + I); 
    } 
    return RES; 
}

 

Guess you like

Origin www.cnblogs.com/sheepcore/p/12524415.html