'Smallest lexographic string

I am trying to solve the following challenge:

You are given a special string S and this string represents relations. String S consists of only two characters "<" and ">". Your task is to find the lexicographically-smallest string L consisting of unique characters of lower case Latin letters (a-z) and which follows the relations mentioned in S.

For example, if you are given S as "<><", then you have to find a lexicographically-smallest string in which every two consecutive characters follow the relation in the order mentioned in S. Here, the answer is "acbd" as a < c > b < d.

It is clear that, if S is of length n, then the length of L is n+1. You have to print string L.

Input format

  • The first line contains T denoting the number of test cases in each test file.
  • The first line of each test case contains string S denoting the relations.


Solution 1:[1]

Some observations:

  • We can extend S with one additional "<", so to make the number of characters in S equal to the expected size of the output.
  • If the ith character in S is a "<", then the corresponding output letter should be the ith letter from the alphabet.
  • The group of output letters that correspond to a consecutive group of ">" in S, is the group of letters you would get if they were a sequence of "<", but then in reverse order.

With these principles in mind, it is straightforward to design the algorithm. In case a ">" is encountered, you just search for how many of those you have in a consecutive series, and produce the reversed block of letters for it. For a "<" character just output the corresponding letter from the alphabet, based on the current index.

Here is an implementation in JavaScript where you can input S, and the output will be displayed in real time:

function minString(s) {
    let letters = "abcdefghijklmnopqrstuvwxyz";
    let res = "";
    s += "<"; // add a stub 
    for (let i = 0; i < s.length; i++) {
        if (s[i] === "<") {
            res += letters[i];
        } else {
            let start = i;
            while (s[i] === ">") i++;
            for (let j = i; j >= start; j--) {
                res += letters[j]; // descending series of letters
            }
        }
    }
    return res;
}

// I/O handling

let input = document.querySelector("input");
let output = document.querySelector("div");
const refresh = () => output.textContent = minString(input.value);
input.addEventListener("input", refresh);
refresh();
input { width:100% }
S: <input value="&lt;&lt;>>">
Output:
<div>

Solution 2:[2]

string solve(string S) {
   stack<char> st;
   string ans;
   for (int i = 0; i <= S.size(); i++) {
      st.push(i + 'a');

   if(i == S.size() || S[i] == '<') {
   while(!st.empty()) {
     ans+=st.top();
     st.pop();
   }
  }
 }
  return ans;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trincot
Solution 2 Yatin Ahuja