当前位置: 首页 > 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;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Java-ByteArrayResource和InputStream
  • RabbitMQ的介绍
  • 深入理解 Go 语言原子内存操作
  • VS工程中的ALL_BUILD、INSTALL、ZERO_CHECK简介
  • NLP位置编码
  • vue3动态引入图片不显示问题
  • [Zer0pts2020]Can you guess it?1
  • python | 字符串编码问题怎么破
  • 在Ubuntu 14.04上安装LAMP【快速入门】
  • Spring Boot发送邮件带附件功能怎么实现?
  • Vim多文件操作
  • 我叫:堆排序【JAVA】
  • 动手学深度学习7.6 残差网络(ResNet)-笔记练习(PyTorch)
  • 【MySQL】数据库约束和多表查询
  • 数学基础 -- 函数的平均值定理与定积分的中值定理
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • angular组件开发
  • Git初体验
  • httpie使用详解
  • iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
  • js对象的深浅拷贝
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • Python - 闭包Closure
  • Python爬虫--- 1.3 BS4库的解析器
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • Redash本地开发环境搭建
  • webpack项目中使用grunt监听文件变动自动打包编译
  • windows下mongoDB的环境配置
  • 基于Android乐音识别(2)
  • 入手阿里云新服务器的部署NODE
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 学习笔记TF060:图像语音结合,看图说话
  • AI算硅基生命吗,为什么?
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #Linux(make工具和makefile文件以及makefile语法)
  • #宝哥教你#查看jquery绑定的事件函数
  • #微信小程序:微信小程序常见的配置传值
  • (二)c52学习之旅-简单了解单片机
  • (分布式缓存)Redis持久化
  • (删)Java线程同步实现一:synchronzied和wait()/notify()
  • (顺序)容器的好伴侣 --- 容器适配器
  • (四)JPA - JQPL 实现增删改查
  • (算法)大数的进制转换
  • (原)Matlab的svmtrain和svmclassify
  • (原創) 物件導向與老子思想 (OO)
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转)平衡树
  • .bat批处理(一):@echo off
  • .NET/C# 使用反射注册事件
  • .NET国产化改造探索(一)、VMware安装银河麒麟
  • .NET开源、简单、实用的数据库文档生成工具
  • ?php echo $logosrc[0];?,如何在一行中显示logo和标题?
  • [.NET 即时通信SignalR] 认识SignalR (一)