Advertisement
jasonpogi1669

Find Lexicographically Smallest String using C++

Feb 5th, 2022
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string GetSmaller(string s1, string s2) {
  6.     int n = (int) s1.size();
  7.     int m = (int) s2.size();
  8.     for (int i = 0; i < min(n, m); i++) {
  9.         if (s1[i] < s2[i]) {
  10.             return s1;
  11.         } else if (s2[i] < s1[i]) {
  12.             return s2;
  13.         }
  14.     }
  15.     return (n < m ? s1 : s2);
  16. }
  17.  
  18. int main() {
  19.     string x, y;
  20.     cin >> x >> y;
  21.     cout << GetSmaller(x, y) << '\n';
  22.     return 0;
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement