Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
its goway names like these
The greater the distance between a name's declaration and its uses,
the longer the name should be.
https://go.dev/talks/2014/names.slide#4
The time complexity of the
longest
method is determined by the operations performed within the method. Let's analyze the operations step by step:Concatenation: The line
String s = s1 + s2;
performs concatenation of the two input stringss1
ands2
. The time complexity of string concatenation in Java is O(n1 + n2), where n1 is the length ofs1
and n2 is the length ofs2
.Distinct: The
distinct()
method is used on theIntStream
created from the concatenated strings
. Thedistinct()
method creates a new stream with distinct elements, which involves iterating through the elements of the stream and keeping track of unique elements. The time complexity of thedistinct()
operation is O(n), where n is the number of characters in the concatenated strings
.Sorting: The
sorted()
method is used to sort the characters in the stream in ascending order. The time complexity of thesorted()
operation is O(n log n), where n is the number of characters in the concatenated strings
.Collecting: The
collect()
method is used to collect the sorted characters back into aStringBuilder
to form the resulting string. Thecollect()
operation iterates through the elements in the stream and appends them to theStringBuilder
. The time complexity of thecollect()
operation is O(n), where n is the number of characters in the concatenated strings
.Overall, the dominant operation is the sorting step with a time complexity of O(n log n). Therefore, the time complexity of the
longest
method is O(n log n), where n is the total number of characters in the concatenated strings
.It's important to note that the time complexity may vary depending on the length of the input strings and the specific implementation of the
distinct()
andsorted()
operations. However, in this case, we can safely say that the time complexity is O(n log n) due to the sorting operation.great and neat solution! Thank you!
One thing to mention is the creating a resulting string. I guess, it's better to write the result to rune slice or buffer. Since, string concat creates new string each time. Giving a considerable overhead
Agree, it's clever to do that. But why is there more of the "best practices" likes rather than "clever"?
Imo: making variable names like these is not one of the "best practices" :)
Hi, guys, what is the time complexity here?
This solution is mind blowing for me. Smart!
This comment is hidden because it contains spoiler information about the solution
Addendum: Codewars on GitHub
Not a kata issue. If the buttons are not visible, try changing the webpage zoom. Otherwise, explain your problem properly, attach a screenshot, or raise an issue on CW github.
Guys, I do not have 'test sample' and submission options. What should I do to fix it?
This comment is hidden because it contains spoiler information about the solution