Given an integer N, the task is to check if N is an zygodrome Number.
Zygodrome Number is a numbers if it is made of nontrivial runs of identical digits.
For example: 112233, 7777333 and 1100 are all zygodromes in base 10.
Examples:
Input: N = 1122
Output: Yes
Input: N = 26
Output: No
Approach: The idea is to convert the number to string and return false if any character is not equal to previous character and next character. Since the first character does not have a previous character and the last character doesn’t have the next character so we will add a space at the beginning and end of the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool iszygodromeNum( int N)
{
string s = to_string(N);
s = ' ' + s + ' ' ;
for ( int i = 1; i < s.size() - 1; i++) {
if (s[i] != s[i - 1]
&& s[i] != s[i + 1]) {
return false ;
}
}
return true ;
}
int main()
{
int n = 1122;
if (iszygodromeNum(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean iszygodromeNum( int N)
{
String s = Integer.toString(N);
s = ' ' + s + ' ' ;
for ( int i = 1 ; i < s.length() - 1 ; i++)
{
if (s.charAt(i) != s.charAt(i - 1 ) &&
s.charAt(i) != s.charAt(i + 1 ))
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
int n = 1122 ;
if (iszygodromeNum(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def iszygodromeNum(N):
s = str (N);
s = ' ' + s + ' ' ;
i = 1
while i < len (s) - 1 :
if ((s[i] ! = s[i - 1 ]) and
(s[i] ! = s[i + 1 ])):
return False ;
i + = 1
return True ;
if __name__ = = '__main__' :
n = 1122 ;
if iszygodromeNum(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool iszygodromeNum( int N)
{
String s = N.ToString();
s = ' ' + s + ' ' ;
for ( int i = 1; i < s.Length - 1; i++)
{
if (s[i] != s[i - 1] &&
s[i] != s[i + 1])
{
return false ;
}
}
return true ;
}
public static void Main(String[] args)
{
int n = 1122;
if (iszygodromeNum(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function iszygodromeNum( N)
{
let s = N.toString();
s = ' ' + s + ' ' ;
for ( i = 1; i < s.length - 1; i++)
{
if (s[i] != s[i-1] && s[i] != s[i + 1])
{
return false ;
}
}
return true ;
}
let n = 1122;
if (iszygodromeNum(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log10n)
Last Updated :
11 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...