https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Social%20Network%20Company/Social%20Network%20Company%20-%20Interview%20Questions%20-%20SOLUTIONS/Phone%20Screen%20-%20SOLUTION.ipynb

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的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. Remove Duplicates from Sorted Array II

    题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  6. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  7. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  8. Remove Duplicates from Sorted Array II [LeetCode]

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  9. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  10. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

随机推荐

  1. 31. centos 下修改oracle的编码

    [root@localhost ~]# su - oracle[oracle@localhost ~]$ vi /home/oracle/.bash_profile # .bash_profile # ...

  2. gevent 实现单线程下的socket链接

    通过gevent实现socket的多并发 server 端: import geventfrom gevent import socket, monkey monkey.patch_all() #进行 ...

  3. AES 加密256位 错误 java.security.InvalidKeyException: Illegal key size or default parameters

    Java发布的运行环境包中的加解密有一定的限制.比如默认不允许256位密钥的AES加解密,解决方法就是修改策略文件. 官方网站提供了JCE无限制权限策略文件的下载: JDK8的下载地址: http:/ ...

  4. hibernate-list

    Hibernate多表关联查询类: 1. sql查询两个或两个以上的字段,默认情况下,list中封装的是Object[],长度与所查询的字段数一致.这种方式获取的数据只能通过index下标获取. su ...

  5. pycharm专业版(window)安装

    1.官网下载 2. 3.直接finlsh 4. 5. https://pan.baidu.com/s/1mQcc98iJS5bnIyrC6097yA   密码:b1c1

  6. 16 MySQL--正确使用索引

    count 统计 count(*)和count(字段名) 基本结果是一样的 但是一种情况例外,就是当某字段名下边的数据有null值的时候,不计入这个count中,*则全部列入count中 一 .索引未 ...

  7. 运行tomcat显示指定的服务未安装解决办法

    一.问题重现 二.原因分析 tomcat7.exe和tomcat7w.exe要起作用必须先未这两个文件安装服务. 其中tomcat7.exe这个文件是用来启动tomcat的,tomcat7w.exe这 ...

  8. ArcGIS 复制要素

    DataManagementTools.General.Copy DataManagementTools.Features.CopyFeatures ConversionTools.ToGeodata ...

  9. NodeJS框架express的路径映射(路由)功能及控制

    我 们知道Express是一个基于NodeJS的非常优秀的服务端开发框架,本篇CSSer将提供express框架的route和route control章节,route实现了客户端请求的URL的路径映 ...

  10. Android使用HTTPS进行IP直连握手失败问题(okHttp)

    为什么要使用ip直连这种方式去请求我们的服务器呢?这其实和国内运营伤有关,运营商有时为了利益会将你的域名劫持换成他人的域名,为了防止这种情况的发生通用的解决办法要么联系运营商要么就只能使用ip直连了. ...