返回介绍

solution / 1000-1099 / 1047.Remove All Adjacent Duplicates In String / README_EN

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

1047. Remove All Adjacent Duplicates In String

中文文档

Description

You are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.

We repeatedly make duplicate removals on s until we no longer can.

Return _the final string after all such duplicate removals have been made_. It can be proven that the answer is unique.

 

Example 1:

Input: s = "abbaca"
Output: "ca"
Explanation: 
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move.  The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".

Example 2:

Input: s = "azxxzy"
Output: "ay"

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.

Solutions

Solution 1

class Solution:
  def removeDuplicates(self, s: str) -> str:
    stk = []
    for c in s:
      if stk and stk[-1] == c:
        stk.pop()
      else:
        stk.append(c)
    return ''.join(stk)
class Solution {
  public String removeDuplicates(String s) {
    StringBuilder sb = new StringBuilder();
    for (char c : s.toCharArray()) {
      if (sb.length() > 0 && sb.charAt(sb.length() - 1) == c) {
        sb.deleteCharAt(sb.length() - 1);
      } else {
        sb.append(c);
      }
    }
    return sb.toString();
  }
}
class Solution {
public:
  string removeDuplicates(string s) {
    string stk;
    for (char c : s) {
      if (!stk.empty() && stk[stk.size() - 1] == c) {
        stk.pop_back();
      } else {
        stk += c;
      }
    }
    return stk;
  }
};
func removeDuplicates(s string) string {
  stk := []rune{}
  for _, c := range s {
    if len(stk) > 0 && stk[len(stk)-1] == c {
      stk = stk[:len(stk)-1]
    } else {
      stk = append(stk, c)
    }
  }
  return string(stk)
}
impl Solution {
  pub fn remove_duplicates(s: String) -> String {
    let mut stack = Vec::new();
    for c in s.chars() {
      if !stack.is_empty() && *stack.last().unwrap() == c {
        stack.pop();
      } else {
        stack.push(c);
      }
    }
    stack.into_iter().collect()
  }
}
/**
 * @param {string} s
 * @return {string}
 */
var removeDuplicates = function (s) {
  const stk = [];
  for (const c of s) {
    if (stk.length && stk[stk.length - 1] == c) {
      stk.pop();
    } else {
      stk.push(c);
    }
  }
  return stk.join('');
};
char* removeDuplicates(char* s) {
  int n = strlen(s);
  char* stack = malloc(sizeof(char) * (n + 1));
  int i = 0;
  for (int j = 0; j < n; j++) {
    char c = s[j];
    if (i && stack[i - 1] == c) {
      i--;
    } else {
      stack[i++] = c;
    }
  }
  stack[i] = '\0';
  return stack;
}

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

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

发布评论

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