LeetCode //C - 316. Remove Duplicate Letters

devtools/2024/9/24 7:05:38/

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.ppmy.cn/devtools/96087.html

相关文章

动态规划(算法篇)

算法之动态规划 动态规划(dp) 概念&#xff1a; 将递归算法重新写成非递归算法&#xff0c;让后者把那些子问题的答案系统地记录在一个表(dp数组)内&#xff0c;这种方法叫做动态规划通常用于求解具有最优性质的问题(最优子结构&最优子问题)&#xff0c;希望找到具有最优…

数据结构队列的单链表实现

1.Queuec.h头文件函数名 #pragma once #include<stdio.h> #include<stdlib.h> #include<stdbool.h> #include<assert.h> typedef int QDataType; typedef struct QueueNode {QDataType data;struct QueueNode* next; }QNode; typedef struct Queue {Q…

MySQL中处理JSON数据:大数据分析的新方向,详解与示例

文章目录 1. MySQL中的JSON数据类型2. JSON函数和运算符3. 创建JSON列的表4. 插入JSON数据5. 查询JSON数据6. 复杂查询和聚合7. JSON 数据的索引8. 总结 在当今的大数据时代&#xff0c;JSON&#xff08;JavaScript Object Notation&#xff09;作为一种轻量级的数据交换格式&a…

【测试用例设计方法】错误猜测法

1.错误推测法的概念 错误推测法就是基于经验和直觉推测程序中所有可能存在的各种错误&#xff0c;有针对性地设计测试用例的方法。 2.错误推断法的基本思想 列举出程序中所有可能有的错误和容易发生错误的特殊情况&#xff0c;根据它们选择测试用例。 3. 错误推测法的应用案例 …

Linux命令更新-网络管理

引言 Linux系统作为一个灵活且强大的操作系统&#xff0c;其网络管理功能也是非常丰富的。本文将深入探讨Linux中常用的网络管理命令&#xff0c;包括ifconfig、ip、route等&#xff0c;并结合实例演示其用法和功能&#xff0c;旨在帮助读者更全面地掌握Linux网络配置与管理。…

面向自动驾驶保证车辆转向稳定性的模型预测控制

摘 要 车辆智能化是当前和未来汽车发展的主要方向和核心技术之一。随着车辆智能化水 平的提高&#xff0c;自动驾驶等级从无自动驾驶向完全自动驾驶提升。在自动驾驶的人机协同控制 和完全自动驾驶阶段&#xff0c;由于人类驾驶员在动态驾驶任务中的参与程度不同&#xff0c;…

SpringCloud基于Eureka的服务治理架构搭建与测试:从服务提供者到消费者的完整流程

Spring Cloud微服务框架中的Eureka是一个用于服务发现和注册的基础组件&#xff0c;它基于RESTful风格&#xff0c;为微服务架构提供了关键的服务注册与发现功能。以下是对Eureka的详细解析和搭建举例。 一. Eureka基础知识 &#xff08;1&#xff09;服务治理 服务治理是微…

Linux 开机自动挂载共享文件设置

选择一个要共享的文件 点击确定 -> 确定 启动虚拟机 执行下面的命令 /YumSource 是我选择的共享文件夹&#xff0c;自行替换自已选择的文件夹 mkdir -p /mnt/hgfs cat >> /etc/fstab << EOF .host:/YumSource /mnt/hgfs fuse.vmhgfs-fuse allow_other defaul…