Check if a string is Colindrome
Given a string, check if it is Colindrome or not. A string is said to be colindrome if it has consecutive 3 alphabets followed by the reverse of these 3 alphabets and so on. Examples :
Input : cappaccappac
Output : String is colindrome
Input : mollomaappaa
Output : String is Colindrome
Approach : Take two empty strings s1 and s2, and start iterating over the given string. Take first three letters of the string and store it in s1, next three chars in s2 and then match s1 and s2. If they are same then do the same again till length of the given string else return false.
C++
#include<bits/stdc++.h>
using namespace std;
bool colindrome(string str)
{
if (str.size()<=3 || str.size()%3!=0)
return false ;
int i = 0,j=i+3;
while (j<str.size())
{
string temp =str.substr(j,3);
reverse(temp.begin(),temp.end());
cout<<str.substr(i,3)<< " " <<temp<<endl;
if (str.substr(i,3)!=temp)
return false ;
i+=6;
j+=6;
}
return true ;
}
int main()
{
string s = "cbbbbc" ;
if (colindrome(s))
cout<< "String is colindrome\n" ;
else
cout<< "Not colindrome" ;
return 0;
}
|
Java
public class Colindrome {
static boolean colindrome(String s)
{
int i1 = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
int i2 = i1 + 3 ;
String s1 = "" ;
String s2 = "" ;
int c1 = 0 , c2 = 0 ;
for (i1 = i1; i1 < s.length(); i1++) {
c1++;
s1 = s1 + s.charAt(i1);
if (c1 == 3 ) {
break ;
}
}
for (i2 = i2; i2 < s.length(); i2++) {
c2++;
s2 = s2 + s.charAt(i2);
if (c2 == 3 ) {
break ;
}
}
String s3 = "" ;
for ( int k = s2.length() - 1 ; k >= 0 ; k--) {
s3 = s3 + s2.charAt(k);
}
if (s1.equals(s3) != true ) {
return false ;
}
i1 = i1 + 6 ;
i2 = i2 + 3 ;
}
return true ;
}
public static void main(String[] args)
{
String s = "cbbbbc" ;
boolean b = colindrome(s);
if (b) {
System.out.println( "String is colindrome" );
}
else {
System.out.println( "Not Colindrome" );
}
}
}
|
Python3
def colindrome(s):
i1 = 0
for i in range ( 0 , len (s)):
i2 = i1 + 3
s1 = ""
s2 = ""
c1 = 0 ; c2 = 0 ;
while (i1 < len (s)):
c1 + = 1
s1 = s1 + s[i1]
if (c1 = = 3 ) : break
i1 + = 1
while (i2 < len (s)):
c2 + = 1
s2 = s2 + s[i2]
if (c2 = = 3 ) : break
i2 + = 1
s3 = ""
for k in range ( len (s2) - 1 , - 1 , - 1 ) :
s3 = s3 + s2[k]
if (s1 ! = s3) :
return False
i1 = i1 + 6
i2 = i2 + 3
return True
if __name__ = = '__main__' :
s = "cbbbbc" ;
if (colindrome(s)) :
print ( "String is colindrome" )
else :
print ( "Not colindrome" )
|
C#
using System;
public class Colindrome
{
static bool colindrome(String s)
{
int i1 = 0;
for ( int i = 0; i < s.Length; i++)
{
int i2 = i1 + 3;
String s1 = "" ;
String s2 = "" ;
int c1 = 0, c2 = 0;
for (i1 = i1; i1 < s.Length; i1++)
{
c1++;
s1 = s1 + s[i1];
if (c1 == 3)
{
break ;
}
}
for (i2 = i2; i2 < s.Length; i2++)
{
c2++;
s2 = s2 + s[i2];
if (c2 == 3)
{
break ;
}
}
String s3 = "" ;
for ( int k = s2.Length - 1; k >= 0; k--)
{
s3 = s3 + s2[k];
}
if (s1.Equals(s3) != true )
{
return false ;
}
i1 = i1 + 6;
i2 = i2 + 3;
}
return true ;
}
public static void Main(String[] args)
{
String s = "cbbbbc" ;
bool b = colindrome(s);
if (b)
{
Console.WriteLine( "String is colindrome" );
}
else
{
Console.WriteLine( "Not Colindrome" );
}
}
}
|
Javascript
function colindrome( s)
{
var i1 = 0;
for ( var i = 0; i < s.length; i++) {
var i2 = i1 + 3;
var s1 = "" ;
var s2 = "" ;
var c1 = 0, c2 = 0;
for (i1 = i1; i1 < s.length; i1++) {
c1++;
s1 = s1 + s[i1];
if (c1 === 3) {
break ;
}
}
for (i2 = i2; i2 < s.length; i2++) {
c2++;
s2 = s2 + s[i2];
if (c2 === 3) {
break ;
}
}
var s3 = "" ;
for ( var k = s2.length - 1; k >= 0; k--) {
s3 = s3 + s2[k];
}
if (s1 != s3) {
return false ;
}
i1 = i1 + 6;
i2 = i2 + 3;
}
return true ;
}
var s = "cbbbbc" ;
if (colindrome(s) == true )
console.log( "String is colindrome\n" );
else
console.log( "Not colindrome" );
|
Output
cbb cbb
String is colindrome
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...