返回介绍

solution / 2800-2899 / 2810.Faulty Keyboard / README_EN

发布于 2024-06-17 01:02:59 字数 4668 浏览 0 评论 0 收藏 0

2810. Faulty Keyboard

中文文档

Description

Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected.

You are given a 0-indexed string s, and you type each character of s using your faulty keyboard.

Return _the final string that will be present on your laptop screen._

 

Example 1:

Input: s = "string"
Output: "rtsng"
Explanation: 
After typing first character, the text on the screen is "s".
After the second character, the text is "st". 
After the third character, the text is "str".
Since the fourth character is an 'i', the text gets reversed and becomes "rts".
After the fifth character, the text is "rtsn". 
After the sixth character, the text is "rtsng". 
Therefore, we return "rtsng".

Example 2:

Input: s = "poiinter"
Output: "ponter"
Explanation: 
After the first character, the text on the screen is "p".
After the second character, the text is "po". 
Since the third character you type is an 'i', the text gets reversed and becomes "op". 
Since the fourth character you type is an 'i', the text gets reversed and becomes "po".
After the fifth character, the text is "pon".
After the sixth character, the text is "pont". 
After the seventh character, the text is "ponte". 
After the eighth character, the text is "ponter". 
Therefore, we return "ponter".

 

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.
  • s[0] != 'i'

Solutions

Solution 1: Simulation

We directly simulate the keyboard input process, using a character array $t$ to record the text on the screen, initially $t$ is empty.

For each character $c$ in string $s$, if $c$ is not the character $'i'$, then we add $c$ to the end of $t$; otherwise, we reverse all characters in $t$.

The final answer is the string composed of characters in $t$.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of string $s$.

class Solution:
  def finalString(self, s: str) -> str:
    t = []
    for c in s:
      if c == "i":
        t = t[::-1]
      else:
        t.append(c)
    return "".join(t)
class Solution {
  public String finalString(String s) {
    StringBuilder t = new StringBuilder();
    for (char c : s.toCharArray()) {
      if (c == 'i') {
        t.reverse();
      } else {
        t.append(c);
      }
    }
    return t.toString();
  }
}
class Solution {
public:
  string finalString(string s) {
    string t;
    for (char c : s) {
      if (c == 'i') {
        reverse(t.begin(), t.end());
      } else {
        t.push_back(c);
      }
    }
    return t;
  }
};
func finalString(s string) string {
  t := []rune{}
  for _, c := range s {
    if c == 'i' {
      for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
        t[i], t[j] = t[j], t[i]
      }
    } else {
      t = append(t, c)
    }
  }
  return string(t)
}
function finalString(s: string): string {
  const t: string[] = [];
  for (const c of s) {
    if (c === 'i') {
      t.reverse();
    } else {
      t.push(c);
    }
  }
  return t.join('');
}
impl Solution {
  pub fn final_string(s: String) -> String {
    let mut t = Vec::new();
    for c in s.chars() {
      if c == 'i' {
        t.reverse();
      } else {
        t.push(c);
      }
    }
    t.into_iter().collect()
  }
}

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

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

发布评论

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