I came across a problem where I am given a string and an integer k, and I have to figure out if using all the characters of string, k palidromes can be constructed or not. I wrote the two different correctly working programs, one using Counter and another using set. I was comparing the time complexity and runing time of each program.
First program
from collections import Counter def canConstruct(self, s: str, k: int) -> bool: if k == len(s): return True if k > len(s): return False hmap = Counter(s) #O(n) odd_count = sum(freq%2!=0 for freq in hmap.values()) #O(m) {assuming there are m unique element} if k < odd_count: return False return True
Run time : 32 ms
Time complexity o(n + m)
Second program
def canConstruct(self, s: str, k: int) -> bool: if k==len(s): return True if k>len(s): return False unique_elements = set(s) #O(n) odd_count = 0 for i in unique_elements: #O(m*r) {assuming average length of string is r) if s.count(i)%2!=0: odd_count+=1 if k < odd_count: return False return True
Run time : 19 ms
Let assume r has constraint which makes O(r) constant Time complexity : O(m+n)
I know that the time complexity and running time are two different things and codes with same time complexities can have different running time.
My question is, why does the second program, which uses set and count, have a better running time than using Counter?
submitted by /u/Nyctophilic_enigma
[link] [comments]
r/learnpython I came across a problem where I am given a string and an integer k, and I have to figure out if using all the characters of string, k palidromes can be constructed or not. I wrote the two different correctly working programs, one using Counter and another using set. I was comparing the time complexity and runing time of each program. First program from collections import Counter def canConstruct(self, s: str, k: int) -> bool: if k == len(s): return True if k > len(s): return False hmap = Counter(s) #O(n) odd_count = sum(freq%2!=0 for freq in hmap.values()) #O(m) {assuming there are m unique element} if k < odd_count: return False return True Run time : 32 ms Time complexity o(n + m) Second program def canConstruct(self, s: str, k: int) -> bool: if k==len(s): return True if k>len(s): return False unique_elements = set(s) #O(n) odd_count = 0 for i in unique_elements: #O(m*r) {assuming average length of string is r) if s.count(i)%2!=0: odd_count+=1 if k < odd_count: return False return True Run time : 19 ms Let assume r has constraint which makes O(r) constant Time complexity : O(m+n) I know that the time complexity and running time are two different things and codes with same time complexities can have different running time. My question is, why does the second program, which uses set and count, have a better running time than using Counter? submitted by /u/Nyctophilic_enigma [link] [comments]
I came across a problem where I am given a string and an integer k, and I have to figure out if using all the characters of string, k palidromes can be constructed or not. I wrote the two different correctly working programs, one using Counter and another using set. I was comparing the time complexity and runing time of each program.
First program
from collections import Counter def canConstruct(self, s: str, k: int) -> bool: if k == len(s): return True if k > len(s): return False hmap = Counter(s) #O(n) odd_count = sum(freq%2!=0 for freq in hmap.values()) #O(m) {assuming there are m unique element} if k < odd_count: return False return True
Run time : 32 ms
Time complexity o(n + m)
Second program
def canConstruct(self, s: str, k: int) -> bool: if k==len(s): return True if k>len(s): return False unique_elements = set(s) #O(n) odd_count = 0 for i in unique_elements: #O(m*r) {assuming average length of string is r) if s.count(i)%2!=0: odd_count+=1 if k < odd_count: return False return True
Run time : 19 ms
Let assume r has constraint which makes O(r) constant Time complexity : O(m+n)
I know that the time complexity and running time are two different things and codes with same time complexities can have different running time.
My question is, why does the second program, which uses set and count, have a better running time than using Counter?
submitted by /u/Nyctophilic_enigma
[link] [comments]