当前位置: 首页 > news >正文

LeetCode //C - 316. Remove Duplicate Letters

316. Remove Duplicate Letters

Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
 

Example 1:

Input: s = “bcabc”
Output: “abc”

Example 2:

Input: s = “cbacdcbc”
Output: “acdb”

Constraints:
  • 1 < = s . l e n g t h < = 1 0 4 1 <= s.length <= 10^4 1<=s.length<=104
  • s consists of lowercase English letters.

From: LeetCode
Link: 316. Remove Duplicate Letters


Solution:

Ideas:

1. lastIndex[]: This array stores the last occurrence of each character in the input string s.

2. seen[]: This boolean array keeps track of which characters are already included in the stack (result).

3. stack: This array is used as a stack to build the result string with the smallest lexicographical order.

4. Algorithm:

  • Traverse through each character in the string s.
  • Skip the character if it is already in the result.
  • Otherwise, pop characters from the stack if they are lexicographically greater than the current character and if they appear later in the string.
  • Push the current character onto the stack and mark it as seen.

5. The final stack contains the result, which is then null-terminated and returned as the result string.

Code:
char* removeDuplicateLetters(char* s) {int len = strlen(s);int lastIndex[26] = {0};  // To store the last occurrence of each characterbool seen[26] = {false};  // To keep track of seen charactersint stackSize = 0;        // To keep track of stack size// Find the last occurrence of each characterfor (int i = 0; i < len; i++) {lastIndex[s[i] - 'a'] = i;}// Array to use as a stackchar* stack = (char*)malloc((len + 1) * sizeof(char));for (int i = 0; i < len; i++) {char current = s[i];if (seen[current - 'a']) continue;  // Skip if character is already in the result// Ensure the smallest lexicographical orderwhile (stackSize > 0 && stack[stackSize - 1] > current && lastIndex[stack[stackSize - 1] - 'a'] > i) {seen[stack[--stackSize] - 'a'] = false;}// Add current character to the stack and mark it as seenstack[stackSize++] = current;seen[current - 'a'] = true;}// Null-terminate the result stringstack[stackSize] = '\0';return stack;
}
http://www.lryc.cn/news/426613.html

相关文章:

  • 【ARM+Codesys 客户案例 】RK3568/A40i/STM32+CODESYS在工厂自动化中的应用:PCB板焊接机
  • 【二分查找】--- 初阶题目赏析
  • 【PostgreSQL003】PostgreSQL数据表空间膨胀,磁盘爆满,应用宕机(经验总结,已更新)
  • C语言第20天笔记
  • 为什么穷大方
  • HiveSQL实战——大数据开发面试高频SQL题
  • RabbitMQ集群 - 普通集群搭建、宕机情况
  • xssDOM型练习
  • python中的gradio使用麦克风时报错
  • Oracle(63)什么是临时表(Temporary Table)?
  • 《Techporters架构搭建》-Day06 国际化
  • Linux ACL 访问控制
  • hg transformers pipeline使用
  • 高性能内存对象缓存
  • 文件上传-CMS文件上传分析
  • 云原生日志Loki
  • 初阶数据结构之直接选择排序和快速排序
  • Java语言程序设计——篇十三(4)
  • 低代码: 组件库测试之渲染和元素获取,触发事件,更新表单,验证事件以及异步请求
  • 银河麒麟服务器操作系统Kylin-Server-V10-SP3-2403-Release-20240426-x86_64安装步骤
  • 2024年电赛H题全开源
  • Docker:宿主机可以ping通外网,docker容器内无法ping通外网之解决方法
  • bootchart抓Android系统启动各阶段性能数据
  • 使用 Node.js 和 Express 框架通过网页访问GPIO和嵌入式 Linux 系统中使用 GSM/3G/4G 模块
  • IT 行业的就业情况
  • 如何快速获取麒麟操作系统版本信息
  • git提交规范检查husky
  • LeetCode 919. 完全二叉树插入器
  • C++密码管理器
  • 算法【Java】 —— 滑动窗口