// solution to leetcode 1451 class Solution { public: static bool cmp(std::pair p1, std::pair p2){ //bool cmp(string p1, string p2){ int len1 = p1.first.length(); int len2 = p2.first.length(); if(len1==len2){ // keep the original order return (p1.second> v1; string word; int index; char c; c=tolower(text[0]); text[0]=c; // store the words and its index into v1. stringstream ss(text); while(ss >> word){ // if there are n words, O(n) v1.push_back({word, index}); index++; } std:sort(v1.begin(), v1.end(), cmp); int size; size = v1.size(); for(int i=0;i