Given a sentence text
(A sentence is a string of space-separated words) in the following format:
- The first letter is in the upper case.
- Each word in
text
are separated by a single space.
Your task is to rearrange the words in the text such that all words are rearranged in increasing order of their lengths. If two words have the same length, arrange them in their original order.
Return the new text following the format shown above.
Example 2:
Input: text = "Keep calm and code on"
Output: "On and keep calm code"
Approach
Java
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class RearrangeWordsSentence {public static void main(String[] args) {String text = "Keep calm and code on";System.out.println(arrangeWords(text));}static String arrangeWords(String s) {int n = s.length();int i = 0;List<Pair> v = new ArrayList<Pair>();while (i < n) {String str = "";while (s.charAt(i) == ' ')i++;int j = i;while (i < n && s.charAt(i) != ' ') {str += s.charAt(i);i++;}i++;v.add(new Pair(str, new int[] { str.length(), j }));}String res = "";Collections.sort(v, new Comparator<Pair>() {@Overridepublic int compare(Pair o1, Pair o2) {if (o1.getSecond()[0] == o2.getSecond()[0]) {if (o1.getSecond()[1] < o2.getSecond()[1])return -1;elsereturn 1;} else if (o1.getSecond()[0] < o2.getSecond()[0])return -1;elsereturn 1;}});for (i = 0; i < v.size() - 1; i++) {if (i == 0) {char x[] = v.get(i).getFirst().toCharArray();if (x[0] >= 'a' && x[0] <= 'z')x[0] = (char) (x[0] - 32);res += new String(x);res += " ";} else {char[] x = v.get(i).getFirst().toCharArray();if (x[0] >= 'A' && x[0] <= 'Z')x[0] = (char) (x[0] + 32);res += new String(x);res += " ";}}char[] x = v.get(v.size() - 1).getFirst().toCharArray();if (x[0] >= 'A' && x[0] <= 'Z')x[0] = (char) (x[0] + 32);res += new String(x);return res;}static class Pair {private String first;private int[] second;public String getFirst() {return first;}public void setFirst(String first) {this.first = first;}public int[] getSecond() {return second;}public void setSecond(int[] second) {this.second = second;}public Pair(String first, int[] second) {super();this.first = first;this.second = second;}}}
C++
#include <bits/stdc++.h>using namespace std;//comparator used for sortingbool static cmp(pair<string, pair<int, int>> a, pair<string, pair<int, int>> b){if (a.second.first == b.second.first)return a.second.second < b.second.second;return a.second.first < b.second.first;}string arrangeWords(string s){int n = s.size();int i = 0;vector<pair<string, pair<int, int>>> v;while (i < n){string str = "";while (s[i] == ' ')i++;int j = i;while (i < n && s[i] != ' '){str += s[i];i++;}i++;v.push_back(make_pair(str, make_pair(str.size(), j)));}string res = "";sort(v.begin(), v.end(), cmp);for (int i = 0; i < v.size() - 1; i++){if (i == 0){string x = v[i].first;if (x[0] >= 'a' && x[0] <= 'z')x[0] = x[0] - 32;res += x;res += " ";}else{string x = v[i].first;if (x[0] >= 'A' && x[0] <= 'Z')x[0] = x[0] + 32;res += x;res += " ";}}string x = v[v.size() - 1].first;if (x[0] >= 'A' && x[0] <= 'Z')x[0] = x[0] + 32;res += x;return res;}int main(){string text = "Keep calm and code on";cout << arrangeWords(text);return 0;}
No comments:
Post a Comment