class Solution
{
public:
bool cmp(string str1, string str2)
{
int sp1, sp2;
for (int i = 0; i < str1.length(); i++)
{
if (str1[i] == ' ')
{
sp1 = i;
break;
}
}
for (int i = 0; i < str2.length(); i++)
{
if (str2[i] == ' ')
{
sp2 = i;
break;
}
}
string s1 = "", s2 = "";
for (int i = sp1 + 1; i < str1.length(); i++)
s1 += str1[i];
for (int i = sp2 + 1; i < str2.length(); i++)
s2 += str2[i];
return s1 <= s2;
}
vector<string> reorderLogFiles(vector<string> &logs)
{
int n = logs.size();
vector<string> ansLog(n);
int start = 0, end = n - 1;
for (auto str : logs)
{
reverse(str.begin(), str.end());
if (str[0] >= 48 && str[0] <= 57)
{
reverse(str.begin(), str.end());
ansLog[end] = str;
end--;
}
else
{
reverse(str.begin(), str.end());
ansLog[start] = str;
start++;
}
}
end++;
reverse(ansLog.begin() + end, ansLog.end());
sort(ansLog.begin(), ansLog.begin() + start, cmp);
return ansLog;
}
};
Can anyone help me why i am getting error in sort(ansLog.begin(), ansLog.begin() + start, cmp); . I am using comparator to sort the vector from begining to index start-1.
Question Link: https://leetcode.com/problems/reorder-data-in-log-files/