Summary
Our class is called Solution, and it has the printLargest method. The method requires two inputs: a reference to a vector of strings called arr and an integer, n, representing the number of elements in the vector.
Solution
//User function template for C++ class Solution{ public: // The main function that returns the arrangement with the largest value as // string. // The function accepts a vector of strings string printLargest(int n, vector<string> &arr) { // code here auto isEqual = [&](string a,string b) -> bool { return a == b; }; sort(arr.begin(),arr.end(),[&](string a,string b) -> bool {
return a + b > b + a;
});
string ans = ""; for(auto &x : arr) ans += x; return ans; } }; |
Explanation
The lambda function isEqual determines whether two strings are equal.
The vector arr is sorted using the sort function. Based on a custom comparison function defined as a lambda function, it sorts the strings in descending order.
By concatenating two strings, a and b, in two different orders (a + b and b + a), the lambda function compares them.
Following sorting, the strings are concatenated and saved in the variable ans in the order that the sorting dictated.
Answered by:> >22bceybpy
Credit:> >GeekforGeeks
Suggested blogs:
>Find the subsequences of string in lexicographically sorted order in C++
>How to detect whether the linked list has a loop using Java
>How to determine the number of valid groupings for a string using Java
>How to mock a constant value in Python?
>Creating a pivot table by 6 month interval rather than year
>How to Install mariaDB client efficiently inside docker?
>Can I call an API to find out the random seed in Python `hash()` function?
>How remove residual lines after merge two regions using Geopandas in python?