找出单词的变位词

假设服务器每次用一个单词pinging我,将这些单词存储在队列中。现在队列中的数据非常庞大,我们只能处理数据一次。给定5个单词,为他们在队列中找出变位词。

suppose i m sitting on a server and several machines are pinging me with a word each and we are storing these words in a Queue.Now,the data stored in the queue is very large and we can process it once only.At the end of the day we are given 5 words and we have to find all the anagrams(for each of the 5 words) present in the queue.


1.对于每个单词,按字母排序-这是HashMap的关键字。值是在队列中的词汇链表。每个排好序的变位词有相同的关键字。

2.对于给定的5个词汇,按字符排序,使用它作为关键字在HashMap中查询,并打印相应链表中的所有词汇。

1. For every word, sort characters - this is a key for HashMap. Value is a linked list with actual words in a queue. (Rule - every anagram sorted produces the same key).
2. For every word out of given 5, sort characters, use it as a key for our HashMap and print out all the values from associated linked list.


如果事先知道好要查询的这些词汇:

1.排序5个词汇中的字符,将排序好的结果放到HashSet中

2.对于每个词汇,将字符排好序并和1中创建的HashSet对比,将合理的结果存储在另一个HashSet中。

3.打印出2中创建的HashSet。

If we'd known the words beforehand we could've:
1. Sort chars in 5 words, put the sorted result in a HashSet
2. For every word, sort characters and check against HashSet created in 1 and store the result in another HashSet.
3. At the end of the day print out contents of HashSet built in (2)


如果单词中有许多多次重复的字母,比如:aaabbc 可以这样存储aaabbc

As an addition,the key of hashmap could be shorter.for example,if the key is aaabbc,we can store as aaabbc.





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值