Simple Js O(n) solution with 1 traverse


  • 0
    C
    var findPermutation = function(s) {
        var result = [];
        var i = 0;
        while (i <= s.length) {
            if (i === s.length || s.charAt(i) === 'I') {
                result.push(i + 1);
            } else {
                var index = i;
                while (s.charAt(i) === 'D') i++;
                for (var j = i + 1; j > index; j--) result.push(j);
            }
            i++;
        }
        return result;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.