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 ...
随机推荐
- jap 事务
事物传播行为介绍: @Transactional(propagation=Propagation.REQUIRED) :如果有事务, 那么加入事务, 没有的话新建一个(默认情况下) @Transact ...
- node 图片上传功能
node 代码: var http = require("http"); var express = require('express') app = express(), for ...
- Activity服务类-5 IdentityService服务类
一.内置用户组(角色)设计表概念 用户和组(或者叫做角色),多对多关联,通过关联表实现 act_id_user 用户表: act_id_group 用户组表: act_id_membership 用户 ...
- mongodb基础学习3-查询的复杂用法
昨天看了一下查询,今天来说下查询的复杂用法,可以类比mysql的查询 $ne:不等于 $gt, $gte, $lt, $lte:大于,大于等于,小于,小于等于 $in $and $nor:相当于上面的 ...
- vnc服务安装与配置
一.Redhat上VNC Server配置 本文以当前Linux系统未安装VNC服务器为基本,如果已安装请跳过第1节! 前提: 1.安装 TigerVNC Server # yum search ti ...
- ADO接口简介
源地址:http://blog.csdn.net/xiaobai1593/article/details/7449151 参考: 1. 百度文库:http://wenku.baidu.com/view ...
- How to Pronounce Ending T Clusters + Homophones — Baking!
How to Pronounce Ending T Clusters + Homophones — Baking! Share Tweet Share Tagged With: ARE Reducti ...
- 第六章 图(d)深度优先搜索
- unix架构
UNIX Kernel(UNIX内核):指挥机器的运行,控制计算机的资源 UNIX Shell(UNIX外壳):是UNIX内核和用户的接口,是UNXI的命令解释器.目前常用的Shell有3种 Bour ...
- repeater嵌套RadioButtonList赋值
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Rep ...