We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Anybody pass all 12 test case? I pass form 1 to 8, but cannot pass from 9: "# 9 3s : Terminated due to timeout." I copy source code to VS and run that test case, It run perfect. My Alg is O(n), do you have any alg better?
I have the same problem. Locally I am able to run all test cases and they produce correct answers within seconds, but here they get terminated due to timeout.
Silly me, I've made a mistake of actually deleting from strings, when all I had to do is count the number of delete operations needed. Once the code was changed to ONLY COUNT, additional test cases were passed easily.
Adding 'c' char to the end is an ugly workaround; the loop should end at (chars.length - 1) instead. Then the currChar will have values from 0 to length - 1 and nextChar from 1 to length.
yes I passed all 12. My Alg is also O(n). So what is the problem you are facing? And what language you are using. You can check my solution as well and let me know where I can see your solution.
Besides the fact that string comparison is obviously linear, you would still have to have a fallback to a O(n) algorithm. Since for an input tending to infinity you would need to use it, the whole algorithm would still be O(n).
First, O(N) is the same as O(N/2). The "O" means "order of", meaning that we ignore the coefficients to the terms (be they polynomial, log, or exponential).
Second, this algorithm is no more efficient than the trivial one. It's also considerably less clear.
While it's true you loop half the number of times, you do two comparisons each time through the loop. So, every character is examined twice (except the ones on the ends).
Those who are timing out are doing something very inefficient, and are not O(N).
This my friend, is not O(n/2). Think of it this way, you are reading al lthe elements in the data set. That is what determines O(). The style of looping is not really a determining factor.
Moreover, O(N/2) is simply O(0.5N) and that is ignored. This notation does not care about the constant. So, a complexity of O(N) and O(10^4N) and O(0.0003N) is all the same- O(N).
try and analyze this. I'm giving you the source code.
include
include
include
include
int main() {
int i,j,c=0,x,k;
char a[100];
scanf("%d",&i);
while(i--)
{ c=0; //refreshing the counter
scanf("%s",a);
x=strlen(a); //length of string
for(j=0;j!=x-1;j++) //no need to traverse till end //but end-1
{
if(a[j] ==a[j+1]) //calc. those which are same
{ c++;
}
}
printf("%d\n",c);
}
return 0;
number = int(raw_input())
for k in range(1,number+1):
string = raw_input().strip()
i = len(string) -1 # scan for two consecutive
del_count = 0 #characters, if found increase
while i >0: #delete count
if string[i] == string[i-1]:
del_count = del_count+1
i = i-1
number = int(raw_input())
for k in range(1,number+1):
string = raw_input().strip()
i = len(string) -1 # scan for two consecutive
del_count = 0 #characters, if found increase
while i >0: #delete count
if string[i] == string[i-1]:
del_count = del_count+1
i = i-1
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
for(;t>0;t--){
StringBuffer a = new StringBuffer(sc.nextLine());
StringBuffer b = new StringBuffer("");
b.append(a.charAt(0));
for(int i=1; i<a.length();i++){
if(b.charAt(b.length()-1)!=a.charAt(i))b.append(a.charAt(i));
}
System.out.println(a.length()-b.length());
}
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int i=0;i
Alternating Characters
You are viewing a single comment's thread. Return to all comments →
Anybody pass all 12 test case? I pass form 1 to 8, but cannot pass from 9: "# 9 3s : Terminated due to timeout." I copy source code to VS and run that test case, It run perfect. My Alg is O(n), do you have any alg better?
I have the same problem. Locally I am able to run all test cases and they produce correct answers within seconds, but here they get terminated due to timeout.
Silly me, I've made a mistake of actually deleting from strings, when all I had to do is count the number of delete operations needed. Once the code was changed to ONLY COUNT, additional test cases were passed easily.
Thanks
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
}
YOUR CODE IS NOT CLEARING ALL THE TEST CASES,,,,,!!!
exactly. you need to keep track at least of a "pivot" char, the last valid one from the previous iteration...
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t; cin >> t; string str;
}
IT's passing all the test cases . Please check again .
should be length -1 in loop !
program outputs wrong answer when cin is replaced by getline(cin,s)
tru story, bro... ))))))
Thanks, I made the same mistake. Got it fixed :D
very true!...i wasted 5 hackos for getting the test case, just had to count!...no manipulation of string required, it simply wastes time!...thanks
Thanks! That was the problem that was failing the test cases due to timeout issue.
Same mistake bro thanks.
Here is the simplest solution (passes all 12 cases ):
Calling this inside the for loop that accepts the Strings worked for me.
for (String input : inputs) i think here there is mistake by using the symbol :
Adding 'c' char to the end is an ugly workaround; the loop should end at (chars.length - 1) instead. Then the currChar will have values from 0 to length - 1 and nextChar from 1 to length.
yes I passed all 12. My Alg is also O(n). So what is the problem you are facing? And what language you are using. You can check my solution as well and let me know where I can see your solution.
how many seconds did the most expensive test take? Me I'm around 0.03s...
my most expensive test takes 0.35s.Did you find a better way to improve your code?
Testcase #10, #11. 0.31 seconds. Are you using Java?
Yep - I did. Mine is O(n) too but I don't think you can do better than that as you'll need to go through the string at least once.
we can do better, may be input will repeat so for that not need to calculate again just show older result :)
It'll be hard to show the equality in less than linear time :)
Besides the fact that string comparison is obviously linear, you would still have to have a fallback to a O(n) algorithm. Since for an input tending to infinity you would need to use it, the whole algorithm would still be O(n).
The better solution is O(n/2). If your program is not optimed, then it "MAY" thow "Timeout" as it exceeds the maximum time limit.
How will you do O(n/2), you still need to go though each character to see if it matches the previous one or not.
https://codepair.hackerrank.com/paper/WV5eMmhM?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6InJhdmlyb3NoYW4iLCJlbWFpbCI6InJhdmlyb3NoYW4udGFsa0BnbWFpbC5jb20ifQ%3D%3D
public class Solution { public static void main(String[] args) {
}
Nice. Thanks for sharing.
Scanner in =new Scanner(System.in); int n=in.nextInt();
No actual deleteions, awesome!
Thanks it helped very much..:)
Your code works but if one of the testcases were something like 'abba' it wouldnt work!!!!
Brilliant..
First, O(N) is the same as O(N/2). The "O" means "order of", meaning that we ignore the coefficients to the terms (be they polynomial, log, or exponential).
Second, this algorithm is no more efficient than the trivial one. It's also considerably less clear.
While it's true you loop half the number of times, you do two comparisons each time through the loop. So, every character is examined twice (except the ones on the ends).
Those who are timing out are doing something very inefficient, and are not O(N).
Westerngravity has the right idea.
@ravisohan, a simple java solution would be this:
It passes all the test cases :)
This my friend, is not O(n/2). Think of it this way, you are reading al lthe elements in the data set. That is what determines O(). The style of looping is not really a determining factor.
Moreover, O(N/2) is simply O(0.5N) and that is ignored. This notation does not care about the constant. So, a complexity of O(N) and O(10^4N) and O(0.0003N) is all the same- O(N).
Here I have a simpler solution although I don't think its all that elegant but passed all cases.
O(n/2) is the same than O(n)
Yes. I have passed all the test cases.
Passed, with C++.
include
include
include
include
int main() {
}
Hey !! even i had the same problem !! I was using java. When i changed my string from Sring class to StringBuffer... it worked.
I know that the reply is 8months late.. :P :P But it may be helpful for others.
i did almost the same thing, im getting timeout for test cases 8-12?
yeah . Mine is O(n).
try and analyze this. I'm giving you the source code.
include
include
include
include
int main() { int i,j,c=0,x,k; char a[100]; scanf("%d",&i);
}
oh my god,is that string consist of the letter only 'A' or 'B'?If you care it,you will fail to pass additional case.That's a bug.
oh my god,is that string consist of the letter only 'A' or 'B'?If you care it,you will fail to pass additional case.That's a bug.
number = int(raw_input()) for k in range(1,number+1): string = raw_input().strip() i = len(string) -1 # scan for two consecutive del_count = 0 #characters, if found increase while i >0: #delete count if string[i] == string[i-1]: del_count = del_count+1 i = i-1
number = int(raw_input()) for k in range(1,number+1): string = raw_input().strip() i = len(string) -1 # scan for two consecutive del_count = 0 #characters, if found increase while i >0: #delete count if string[i] == string[i-1]: del_count = del_count+1 i = i-1
i was concatenating the strings for testing purpose and that happened. The I delete those lines and passed all the test cases
Scanner in =new Scanner(System.in); int n=in.nextInt();
It's not working for me ...
This will work time complexity O(N) Auxiliary Space O(N)
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0;i
Here is mine! passed all test cases.