返回介绍

solution / 1400-1499 / 1451.Rearrange Words in a Sentence / README_EN

发布于 2024-06-17 01:03:19 字数 4977 浏览 0 评论 0 收藏 0

1451. Rearrange Words in a Sentence

中文文档

Description

Given a sentence text (A _sentence_ is a string of space-separated words) in the following format:

  • First letter is in upper case.
  • Each word in text are separated by a single space.

Your task is to rearrange the words in text such that all words are rearranged in an 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 1:

Input: text = "Leetcode is cool"
Output: "Is cool leetcode"
Explanation: There are 3 words, "Leetcode" of length 8, "is" of length 2 and "cool" of length 4.
Output is ordered by length and the new first word starts with capital letter.

Example 2:

Input: text = "Keep calm and code on"
Output: "On and keep calm code"
Explanation: Output is ordered as follows:
"On" 2 letters.
"and" 3 letters.
"keep" 4 letters in case of tie order by position in original text.
"calm" 4 letters.
"code" 4 letters.

Example 3:

Input: text = "To be or not to be"
Output: "To be or to be not"

 

Constraints:

  • text begins with a capital letter and then contains lowercase letters and single space between words.
  • 1 <= text.length <= 10^5

Solutions

Solution 1

class Solution:
  def arrangeWords(self, text: str) -> str:
    words = text.split()
    words[0] = words[0].lower()
    words.sort(key=len)
    words[0] = words[0].title()
    return " ".join(words)
class Solution {
  public String arrangeWords(String text) {
    String[] words = text.split(" ");
    words[0] = words[0].toLowerCase();
    Arrays.sort(words, Comparator.comparingInt(String::length));
    words[0] = words[0].substring(0, 1).toUpperCase() + words[0].substring(1);
    return String.join(" ", words);
  }
}
class Solution {
public:
  string arrangeWords(string text) {
    vector<string> words;
    stringstream ss(text);
    string t;
    while (ss >> t) {
      words.push_back(t);
    }
    words[0][0] = tolower(words[0][0]);
    stable_sort(words.begin(), words.end(), [](const string& a, const string& b) {
      return a.size() < b.size();
    });
    string ans = "";
    for (auto& s : words) {
      ans += s + " ";
    }
    ans.pop_back();
    ans[0] = toupper(ans[0]);
    return ans;
  }
};
func arrangeWords(text string) string {
  words := strings.Split(text, " ")
  words[0] = strings.ToLower(words[0])
  sort.SliceStable(words, func(i, j int) bool { return len(words[i]) < len(words[j]) })
  words[0] = strings.Title(words[0])
  return strings.Join(words, " ")
}
function arrangeWords(text: string): string {
  let words: string[] = text.split(' ');
  words[0] = words[0].toLowerCase();
  words.sort((a, b) => a.length - b.length);
  words[0] = words[0].charAt(0).toUpperCase() + words[0].slice(1);
  return words.join(' ');
}
/**
 * @param {string} text
 * @return {string}
 */
var arrangeWords = function (text) {
  let arr = text.split(' ');
  arr[0] = arr[0].toLocaleLowerCase();
  arr.sort((a, b) => a.length - b.length);
  arr[0] = arr[0][0].toLocaleUpperCase() + arr[0].substr(1);
  return arr.join(' ');
};
class Solution {
  /**
   * @param String $text
   * @return String
   */
  function arrangeWords($text) {
    $text = lcfirst($text);
    $arr = explode(' ', $text);
    for ($i = 0; $i < count($arr); $i++) {
      $hashtable[$i] = strlen($arr[$i]);
    }
    asort($hashtable);
    $key = array_keys($hashtable);
    $rs = [];
    for ($j = 0; $j < count($key); $j++) {
      array_push($rs, $arr[$key[$j]]);
    }
    return ucfirst(implode(' ', $rs));
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文