Remove duplicates
Phone Screen - SOLUTION
Problem
Remove duplicate characters in a given string keeping only the first occurrences. For example, if the input is ‘tree traversal’ the output will be ‘tre avsl’.
Requirements¶
Complete this problem on a text editor that does not have syntax highlighting, such as a goolge doc!
Solution
We need a data structure to keep track of the characters we have seen so far, which can perform efficient find operation. If the input is guaranteed to be in standard ASCII form, we can just create a boolean array of size 128 and perform lookups by accessing the index of the character’s ASCII value in constant time. But if the string is Unicode then we would need a much larger array of size more than 100K, which will be a waste since most of it would generally be unused.
Set data structure perfectly suits our purpose. It stores keys and provides constant time search for key existence. So, we’ll loop over the characters of the string, and at each iteration we’ll check whether we have seen the current character before by searching the set. If it’s in the set then it means we’ve seen it before, so we ignore it. Otherwise, we include it in the result and add it to the set to keep track for future reference. The code is easier to understand:
def removeDuplicates(string):
result=[]
seen=set() for char in string:
if char not in seen:
seen.add(char)
result.append(char) return ''.join(result)
The time complexity of the algorithm is O(N) where N is the number of characters in the input string, because set supports O(1) insert and find. This is an optimal solution to one of the most common string interview questions.
This problem should have felt very similar to some other array questions you've been asked! Remember that many basic interview question ideas overlap, just their presentation is different!
Good Job!
Remove duplicates的更多相关文章
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- JAVA WebSocKet ( 简单的聊天室 )
1, 前端代码 登入页 -> login.html <!DOCTYPE html> <html> <head> <meta charset=" ...
- leetcode367
public class Solution { public bool IsPerfectSquare(int num) { , high = num; while (low <= high) ...
- leetcode202
public class Solution { private int SumSqares(int n) { //将一个数字的各个数位的值分开存储 var list = new List<int ...
- Spring boot&Mybatis 启动报错 Failed to auto-configure a DataSource
*************************** APPLICATION FAILED TO START *************************** Description: Fai ...
- php缓存类
<?php /* * 缓存类 cache * 实 例: include( "cache.php" ); $cache = new cache(30); $cache-> ...
- ABAP-ALV报表导出格式恢复初始画面
进入一个ALV表格,想下载数据,一般点清单-->输出-->电子数据表. 会出来一个对话框,可选择导出成各类格式. 在下端有一个“始终使用选定的格式”,一旦勾上,就再也不会弹出选择框了. 以 ...
- ABAP-关于 LUW
转载:https://www.cnblogs.com/liaojunbo/archive/2011/07/12/2103554.html 假设MAIN PROGRAM(调用程序)为MAIN,其所在的为 ...
- .NET MVC 两种视图引擎(Razor、Aspx)
ASPX 优点: 通过上面小小的对比,不难看出,与ASP.NET MVC紧密集成,对于以往ASP.NET开发人员有更好体验.其实它还有其他几优点: ●智能感应 ...
- Java学习 第二节
1.非递归求第四十个斐波那契数 package test; public class fibonacci2 { public static void main(String arg[]) { ; ; ...
- 大型运输行业实战_day09_1_日期转换与My97DatePicker插件使用
1.日期转换 1.1字符串类型转换成时间Date类型 /** * 给定字符串 转变 为 Date 类型 * @param date 时间 * @param format 时间格式 如:yyyy-MM- ...