I was reading the solution of the question asked in Google Kickstart 2022 titled Palindrome Free Strings . The problem statement can be found in the link (https://codingcompetitions.withgoogle.com/kickstart/round/00000000008cb33e/00000000009e762e). Below is the correct C++ code that passed all the tests.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
bool palindrome(string a){
ll i, l, f=0;
l=a.size();
if(l<=4){
return true;
}else{
l=a.size();
for(i=0; i<l/2; i++){
if(a[i]!=a[l-i-1]){
f++;
break;
}
}
}
//cout<<a<<" "<<f<<"\n";
if(f==0){
return true;
}else{
return false;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t, n, t1, i, j, l, f;
string a, b, c;
cin>>t;
for(t1=1; t1<=t; t1++){
cin>>n;
cin>>a;
f=0;
queue<string> q1, q2;
cout<<"Case #"<<t1<<": ";
if(n<5){
cout<<"POSSIBLE\n";
continue;
}
if(a[0]=='?'){
q1.push("1");
q1.push("0");
}else if(a[0]=='0'){
q1.push("0");
}else{
q1.push("1");
}
for(i=1; i<n; i++){
while(q1.empty()==false){
b=q1.front();
q1.pop();
l=b.size()+1;
if(a[i]=='?'){
c=b+"0";
if(l<=4){
q2.push(c);
}else if(l==5){
if(palindrome(c)==false){
q2.push(c);
}
}else{
if(palindrome(c)==false && palindrome(c.substr(1, l-1))==false){
q2.push(c.substr(1, l-1));
}
}
c=b+"1";
if(l<=4){
q2.push(c);
}else if(l==5){
if(palindrome(c)==false){
q2.push(c);
}
}else{
if(palindrome(c)==false && palindrome(c.substr(1, l-1))==false){
q2.push(c.substr(1, l-1));
}
}
}else{
c=b+a[i];
if(l<=4){
q2.push(c);
}else if(l==5){
if(palindrome(c)==false){
q2.push(c);
}
}else{
if(palindrome(c)==false && palindrome(c.substr(1, l-1))==false){
q2.push(c.substr(1, l-1));
}
}
}
}
//cout<<i<<"\n";
//cout<<q2.size()<<"\n";
while(q2.empty()==false){
q1.push(q2.front());
//cout<<q2.front()<<"\n";
q2.pop();
}
//cout<<"\n";
if(q1.empty()==true){
f++;
break;
}
}
if(f==1){
cout<<"IMPOSSIBLE\n";
}else{
cout<<"POSSIBLE\n";
}
}
}
I tried writing the above program in Java with same logic but it only passes the sample and not the test set . can you point me where I am going wrong ?
import java.util.Scanner;
import java.lang.Exception;
import java.lang.StringBuilder;
import java.util.ArrayList;
import java.util.*;
import java.util.stream.Collectors;
public class Solution
{
public static boolean palindrome(String a)
{
int i = 0;
int l = 0;
int f = 0;
l = a.length();
if (l <= 4)
return true;
else
{
for (i = 0; i < l/2; i++)
{
if (a.charAt(i) != a.charAt(l-i-1))
{
f++;
break;
}
}
}
if (f == 0)
return true;
else
return false;
}
public static void checkPalinfree(int first, String second, int input)
{
Queue<String> q1 = new LinkedList<>();
Queue<String> q2 = new LinkedList<>();
String b = "";
String c = "";
int l = 0;
int f = 0;
if (first < 5)
System.out.println("Case #" + (input + 1) + ": " + "POSSIBLE");
if (second.charAt(0) == '?')
{
q1.add("1");
q1.add("0");
}
else if (second.charAt(0) == '0')
{
q1.add("0");
}
else
{
q1.add("1");
}
for (int i = 1; i < first; i++)
{
while (q1.size() != 0)
{
b = q1.peek();
q1.poll();
l = b.length() + 1;
if (second.charAt(i) == '?')
{
c = b + "0";
if (l <= 4)
{
q2.add(c);
}
else if (l == 5)
{
if (palindrome(c) == false)
{
q2.add(c);
}
}
else
{
if (palindrome(c) == false && palindrome(c.substring(1, l-1)) == false)
{
q2.add(c.substring(1,l-1));
}
}
c = b + "1";
if (l <= 4)
{
q2.add(c);
}
else if (l == 5)
{
if (palindrome(c) == false)
{
q2.add(c);
}
}
else
{
if (palindrome(c) == false && palindrome(c.substring(1, l-1)) == false)
{
q2.add(c.substring(1,l-1));
}
}
}
else
{
c = b + second.charAt(i);
if (l <= 4)
{
q2.add(c);
}
else if (l == 5)
{
if (palindrome(c) == false)
{
q2.add(c);
}
}
else
{
if (palindrome(c) == false && palindrome(c.substring(1, l-1)) == false)
{
q2.add(c.substring(1,l-1));
}
}
}
}
while (q2.size() != 0)
{
q1.add(q2.peek());
q2.poll();
}
if (q1.size() == 0)
{
f++;
break;
}
}
if (f == 1)
{
System.out.println("Case #" + (input + 1) + ": " + "IMPOSSIBLE");
}
else
{
System.out.println("Case #" + (input + 1) + ": " + "POSSIBLE");
}
}
public static void main(String args[]) {
Scanner myObj = new Scanner(System.in);
int input_test_case = Integer.parseInt(myObj.nextLine());
for (int i = 0; i < input_test_case; i++)
{
int first = Integer.parseInt(myObj.nextLine());
String second = myObj.nextLine();
checkPalinfree(first,second,i);
}
}
}
Aucun commentaire:
Enregistrer un commentaire