func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MAXN = 100500;   int n, m;   int a[30];   string s, t;   char ans[5500];   void po_poryadky(int x) {     for (int i = x; i < n; i++) {       for (int j = 0; j < 30; j++)         if (a[j]) {           ans[i] = (char)( a  + j);           a[j]--;           break;         }     }   }   void ubiraem(int x) {     for (int i = x; i >= 0; i--) {       int num = ans[i] -  a ;       a[num]++;       for (int j = num + 1; j < 30; j++)         if (a[j]) {           ans[i] = (char)( a  + j);           a[j]--;           po_poryadky(i + 1);           return;         }     }     cout << -1;     exit(0);   }   void v_roven() {     for (int i = 0; i < m; i++) {       int num = t[i] -  a ;       if (a[num] != 0) {         ans[i] = t[i];         a[num]--;         continue;       }       for (int j = num + 1; j < 30; j++)         if (a[j]) {           a[j]--;           ans[i] = (char)( a  + j);           po_poryadky(i + 1);           return;         }       ubiraem(i - 1);       return;     }     if (n == m)       ubiraem(m - 1);     else       po_poryadky(m);   }   int main() {     cin >> s >> t;     n = s.size();     m = t.size();     for (int i = 0; i < n; i++) a[(int)(s[i] -  a )]++;     v_roven();     ans[n] =   0 ;     cout << ans;     return 0;   }   