赞
踩
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.
Input: s = “bcabc”
Output: “abc”
Input: s = “cbacdcbc”
Output: “acdb”
From: LeetCode
Link: 316. Remove Duplicate Letters
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:
5. The final stack contains the result, which is then null-terminated and returned as the result string.
char* removeDuplicateLetters(char* s) { int len = strlen(s); int lastIndex[26] = {0}; // To store the last occurrence of each character bool seen[26] = {false}; // To keep track of seen characters int stackSize = 0; // To keep track of stack size // Find the last occurrence of each character for (int i = 0; i < len; i++) { lastIndex[s[i] - 'a'] = i; } // Array to use as a stack char* 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 order while (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 seen stack[stackSize++] = current; seen[current - 'a'] = true; } // Null-terminate the result string stack[stackSize] = '\0'; return stack; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。