
What is the Time Complexity?
In computer science, the time complexity is the computational complexity that describes the amount of time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform.
The above definition is actually what is written on the text books but I plan to describe the same concept with a very simple example of Palindrome Permutation algorithm in PHP.
A String is called Palindrome if it reads the same backwards as well as forwards. For example, the String can be read the same backwards as well as forwards like “tacocat”.
Now, a Permutation of a String S is some String K where S and K contain the same set of characters, however, these characters need not necessarily have the same positions. For Example, consider the String ‘abc’. Here, the Strings:
- acb
- bca
- bac
- cab
- cba
It is clear that a Palindrome string can have number of even occurrence of any characters. but it only can have odd number of occurrence of a specific characters one time in the middle. so we can have the below algorithm to find whether a string is Palindrome Permutation or not.
<?php
function canBeTurnedIntoAPalindrome($drome) {
// If we’ve found a letter that has no match,
//the center letter.
$centerUsed = false;
$center=”;
$c=”;
$count = 0;
// TODO: Remove whitespace from the string.
// Check each letter to see if there’s
// an even number of it.
for($i = 0; $i<strlen($drome); $i++) {
$c = $drome[$i];
$count = 0;
for($j = 0; $j<strlen($drome); $j++) {
if ($drome[$j] == $c){
$count++;
}
}
// If there was an odd number of those entries
// and the center is already used,
//then a palindrome
// is impossible, so return false.
if ($count % 2 == 1) {
if ($centerUsed == true && $center != $c) return false;
else {
$centerUsed = true;
$center = $c;
// This is so when we encounter it again it
// doesn’t count it as another separate center.
}
}
}
// If we made it all the way through that loop without returning false, then return true;
}
$a=’aaaaaabc’;
if(canBeTurnedIntoAPalindrome($a)){
echo ‘yes’;
} else {
echo ‘no’;
}
The above algorithm is not efficient enough because it has 2 nested loop which makes the Time complexity to O(n^2) means that if the string length would be n then the algorithm should be repeated nxn times.
If you see the below algorithm. It has two loops but not nested so it repeates maximum n+n times which is very less specially if the n would be a big value.
<?php
function canBeTurnedIntoAPalindrome($drome) {
if($drome==null) {
return true;
}
$numberOfOddChars = 0;
for($i = 0; $i<strlen($drome); $i++) {
$c = $drome[$i];
if(isset($chrCounts[$c])) {
$chrCounts[$c]++;
} else {
$chrCounts[$c]=1;
}
}
foreach($chrCounts as $chrCount ) {
if ( $chrCount & 1 ) {
//odd
$numberOfOddChars++;
}
}
if($numberOfOddChars<=1) {
return true;
} else {
return false;
}
}
$a=”;
if(canBeTurnedIntoAPalindrome($a)) {
echo ‘yes’;
} else {
echo ‘no’;
}
Much more efficient with a better Time complexity. That’s all!
I don’t typically comment on posts, but as a long time reader I thought I’d drop in and
wish you all the best during these troubling times.
From all of us at Royal CBD, I hope you stay well with the
COVID19 pandemic progressing at an alarming rate.
Justin Hamilton
Royal CBD
For most up-to-date information you have to go to see the
web and on web I found this web site as a finest web page for latest updates.
It’s amazing to go to see this website and reading the views
of all colleagues about this paragraph, while I am also zealous
of getting familiarity.
This is my first time pay a visit at here and i am truly happy to read everthing at
one place.
I know this website offers quality depending content and
extra data, is there any other web site which provides these information in quality?
Very nice article, totally what I was looking for.
Your means of explaining the whole thing in this post is
really fastidious, all can effortlessly understand it, Thanks a
lot.
It’s very effortless to find out any topic on web
as compared to textbooks, as I found this post at this website.
delta 8 area 52 – delta 8 area 52
delta 8 area 52 – delta 8 THC for sale area 52
area 52 delta 8 THC products – delta 8 carts Area 52
Area 52 delta 8 carts – delta 8 THC area 52
area 52 delta 8 THC products – buy delta 8 THC area 52
Hi there, this weekend is fastidious in favor of me, since
this point in time i am reading this enormous educational paragraph here at my home.
My website: delta 8 gummies
Excellent goods from you, man. I’ve bear in mind your stuff
prior to and you are just extremely magnificent. I really
like what you’ve obtained right here, certainly like what you’re saying and the way in which you are saying it.
You make it entertaining and you still care for to stay it wise.
I can’t wait to learn far more from you. This is really a terrific web site.
Also visit my web page: maeng da botanical specimen capsules