返回介绍

solution / 0400-0499 / 0418.Sentence Screen Fitting / README_EN

发布于 2024-06-17 01:04:00 字数 4287 浏览 0 评论 0 收藏 0

418. Sentence Screen Fitting

中文文档

Description

Given a rows x cols screen and a sentence represented as a list of strings, return _the number of times the given sentence can be fitted on the screen_.

The order of words in the sentence must remain unchanged, and a word cannot be split into two lines. A single space must separate two consecutive words in a line.

 

Example 1:

Input: sentence = ["hello","world"], rows = 2, cols = 8
Output: 1
Explanation:
hello---
world---
The character '-' signifies an empty space on the screen.

Example 2:

Input: sentence = ["a", "bcd", "e"], rows = 3, cols = 6
Output: 2
Explanation:
a-bcd- 
e-a---
bcd-e-
The character '-' signifies an empty space on the screen.

Example 3:

Input: sentence = ["i","had","apple","pie"], rows = 4, cols = 5
Output: 1
Explanation:
i-had
apple
pie-i
had--
The character '-' signifies an empty space on the screen.

 

Constraints:

  • 1 <= sentence.length <= 100
  • 1 <= sentence[i].length <= 10
  • sentence[i] consists of lowercase English letters.
  • 1 <= rows, cols <= 2 * 104

Solutions

Solution 1

class Solution:
  def wordsTyping(self, sentence: List[str], rows: int, cols: int) -> int:
    s = " ".join(sentence) + " "
    m = len(s)
    cur = 0
    for _ in range(rows):
      cur += cols
      if s[cur % m] == " ":
        cur += 1
      while cur and s[(cur - 1) % m] != " ":
        cur -= 1
    return cur // m
class Solution {
  public int wordsTyping(String[] sentence, int rows, int cols) {
    String s = String.join(" ", sentence) + " ";
    int m = s.length();
    int cur = 0;
    while (rows-- > 0) {
      cur += cols;
      if (s.charAt(cur % m) == ' ') {
        ++cur;
      } else {
        while (cur > 0 && s.charAt((cur - 1) % m) != ' ') {
          --cur;
        }
      }
    }
    return cur / m;
  }
}
class Solution {
public:
  int wordsTyping(vector<string>& sentence, int rows, int cols) {
    string s;
    for (auto& t : sentence) {
      s += t;
      s += " ";
    }
    int m = s.size();
    int cur = 0;
    while (rows--) {
      cur += cols;
      if (s[cur % m] == ' ') {
        ++cur;
      } else {
        while (cur && s[(cur - 1) % m] != ' ') {
          --cur;
        }
      }
    }
    return cur / m;
  }
};
func wordsTyping(sentence []string, rows int, cols int) int {
  s := strings.Join(sentence, " ") + " "
  m := len(s)
  cur := 0
  for i := 0; i < rows; i++ {
    cur += cols
    if s[cur%m] == ' ' {
      cur++
    } else {
      for cur > 0 && s[(cur-1)%m] != ' ' {
        cur--
      }
    }
  }
  return cur / m
}
function wordsTyping(sentence: string[], rows: number, cols: number): number {
  const s = sentence.join(' ') + ' ';
  let cur = 0;
  const m = s.length;
  for (let i = 0; i < rows; ++i) {
    cur += cols;
    if (s[cur % m] === ' ') {
      ++cur;
    } else {
      while (cur > 0 && s[(cur - 1) % m] !== ' ') {
        --cur;
      }
    }
  }
  return Math.floor(cur / m);
}

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

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

发布评论

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