Initialize a string that fills 'a' in it. Then we turn it to the expected string from end to start.

The maximal value of each position in the string is 26. If we start from all elements is 'a' in the string. Then the represent value of the string is n. If it’s not equal to k. Then we need turn the last character of string to r = k - n. Two cases must be handled:

  1. Fill z and continue, if r is greater than 25(25 as there is already a 'a' character there).
  2. Fill the corresponding character and done, if r is less than or equal 25.
class Solution {
public:
	string getSmallestString(int n, int k) {
		string r(n, 'a');
		int j = n - 1;
		k -= r.size();
		while (k > 0) {
			if (k > 25) {
				k -= 25;
				r[j--] = 'z';
			} else {
				r[j] = 'a' + k;
				k = 0;
			}
		}
		return r;
	}
};