简体   繁体   English

数数 O(n)中的单词数

[英]Count no. of words in O(n)

I am on an interview ride here. 我在这里接受采访。 One more interview question I had difficulties with. 我遇到了另一个面试问题。

“A rose is a rose is a rose” Write an algorithm that prints the number of times a character/word occurs. “一朵玫瑰是一朵玫瑰是一朵玫瑰”编写一种算法,打印出一个字符/单词出现的次数。 Eg A – 3 Rose – 3 Is – 2 例如A – 3玫瑰– 3是– 2

Also ensure that when you are printing the results, they are in order of what was present in the original sentence. 另外,在打印结果时,请确保它们与原始句子中显示的顺序一致。 All this in order n . 所有这些按顺序n

I did get solution to count number of occurrences of each word in sentence in the order as present in the original sentence. 我确实得到了解决方案,可以按照原始句子中显示的顺序对句子中每个单词出现的次数进行计数。 I used Dictionary<string,int> to do it. 我用Dictionary<string,int>来做到这一点。 However I did not understand what is meant by order of n. 但是我不明白n阶的含义。 That is something I need an explanation from you guys. 那是我需要你们的解释。

There are 26 characters, So you can use counting sort to sort them, in your counting sort you can have an index which determines when specific character visited first time to save order of occurrence. 有26个字符,因此您可以使用counting sort对它们进行排序,在您的计数排序中,您可以拥有一个索引,该索引确定何时首次访问特定字符以保存出现的顺序。 [They can be sorted by their count and their occurrence with sort like radix sort]. [它们可以按计数和出现的顺序进行排序,例如基数排序]。

Edit: by words first thing every one can think about it, is using Hash table and insert words in hash, and in this way count them, and They can be sorted in O(n), because all numbers are within 1..n steel you can sort them by counting sort in O(n), also for their occurrence you can traverse string and change position of same values. 编辑:通过单词,每个人都可以想到的第一件事,就是使用哈希表并在哈希中插入单词,并以此方式对它们进行计数,并且可以将它们按O(n)排序,因为所有数字都在1..n之内您可以通过计算O(n)中的排序来对它们进行排序,对于它们的出现,您还可以遍历字符串并更改相同值的位置。

Order of n means you traverse the string only once or some lesser multiple of n ,where n is number of characters in the string. n的顺序意味着您只能遍历一次字符串或n的较小倍数,其中n是字符串中的字符数。 So your solution to store the String and number of its occurences is O(n) , order of n, as you loop through the complete string only once. 因此,存储String的解决方案及其出现的次数是O(n),即n的顺序,因为您只循环遍历整个字符串一次。 However it uses extra space in form of the list you created. 但是,它以您创建的列表的形式使用额外的空间。

Order N refers to the Big O computational complexity analysis where you get a good upper bound on algorithms. 阶数N是指Big O计算复杂度分析,您可以在其中获得很好的算法上限。 It is a theory we cover early in a Data Structures class, so we can torment, I mean help the student gain facility with it as we traverse in a balanced way, heaps of different trees of knowledge, all different. 这是我们在数据结构课程中较早讲到的一种理论,所以我们可以折磨,我的意思是帮助我们在以平衡的方式遍历时,通过不同的知识树堆,帮助学生获得便利。 In your case they want your algorithm to grow in compute time proportional to the size of the text as it grows. 在您的情况下,他们希望您的算法在计算时间上的增长与文本的大小成正比。

It's a reference to Big O notation . 它是对Big O符号的引用。 Basically the interviewer means that you have to complete the task with an O(N) algorithm. 基本上,面试官意味着您必须使用O(N)算法来完成任务。

"Order n" is referring to Big O notation . “订单n”是指Big O符号 Big O is a way for mathematicians and computer scientists to describe the behavior of a function. Big O是数学家和计算机科学家描述函数行为的一种方式。 When someone specifies searching a string "in order n", that means that the time it takes for the function to execute grows linearly as the length of that string increases. 当有人指定以“顺序n”搜索字符串时,这意味着函数执行所需的时间随着该字符串的长度增加而线性增加。 In other words, if you plotted time of execution vs length of input, you would see a straight line. 换句话说,如果绘制执行时间与输入长度的关系图,则会看到一条直线。

Saying that your function must be of Order n does not mean that your function must equal O(n), a function with a Big O less than O(n) would also be considered acceptable. 说您的函数必须为n阶并不意味着您的函数必须等于O(n),Big O小于O(n)的函数也被认为是可接受的。 In your problems case, this would not be possible (because in order to count a letter, you must "touch" that letter, thus there must be some operation dependent on the input size). 在遇到问题的情况下,这是不可能的(因为要计算字母,您必须“触摸”该字母,因此必须根据输入大小进行一些操作)。

One possible method is to traverse the string linearly. 一种可能的方法是线性遍历字符串。 Then create a hash and list. 然后创建一个哈希和列表。 The idea is to use the word as the hash key and increment the value for each occurance. 想法是使用单词作为哈希键,并为每次出现增加值。 If the value is non-existent in the hash, add the word to the end of the list. 如果哈希中不存在该值,则将该单词添加到列表的末尾。 After traversing the string, go through the list in order using the hash values as the count. 遍历字符串后,使用哈希值作为计数顺序浏览列表。

The order of the algorithm is O(n). 该算法的顺序为O(n)。 The hash lookup and list add operations are O(1) (or very close to it). 哈希查找和列表添加操作为O(1)(或非常接近)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数数 令牌化后的令牌数量,停用词删除和词干 - Count no. of tokens after tokenization, stop words removal and stemming 按出现次数显示前{n}个单词,并包含计数 - Show top {n} words by occurrence and include count 是否可以在 O(n) 中计算字符串中不同子串的数量? - Is it possible to count the number of distinct substrings in a string in O(n)? 如何在O(n)的后续子字符串中计算匹配字符 - How to count matching characters in subsequent substring in O(n) 在小于 o(n^2) 的时间内使用常量空间交换 char* 中的两个单词 - Swap two words in a char* using constant space in less than o(n^2) time 当它在整个句子上调用反向然后在每个单词上反向时,如何解决句子O(N)中的反向单词? - How is this solution to reversing words in a sentence O(N) when it calls reverse on the whole sentence and then reverse on each word? 数数 字符串中出现char的次数 - To count the no. of times a char occur in a string 该算法的时间复杂度:是O(n ^ 2)还是O(n) - Time Complexity of this Algorithm: is it O(n^2) or O(n) 该函数的时间复杂度是O(N)还是O(N ^ 2)? - Is the time complexity for this function O(N) or O(N^2)? 是否存在O(n)算法,用于从输入字符串中查找包含给定字符集和计数的最小子字符串? - Does there exist an O(n) algorithm for finding the smallest substring from an input string, containing a given character set and count?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM