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++ implementation to check if N // is an zygodrome number #include <bits/stdc++.h> using namespace std;
// Function to check if N // is an zygodrome number bool iszygodromeNum( int N)
{ // convert N to string
string s = to_string(N);
// Adding a space at the
// beginning and
// end of the string
s = ' ' + s + ' ' ;
// Traverse the string
for ( int i = 1; i < s.size() - 1; i++) {
// If any character is not same as
// prev and next then return false
if (s[i] != s[i - 1]
&& s[i] != s[i + 1]) {
return false ;
}
}
return true ;
} // Driver code int main()
{ int n = 1122;
if (iszygodromeNum(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
} |
// Java implementation to check if N // is a zygodrome number class GFG{
// Function to check if N // is an zygodrome number static boolean iszygodromeNum( int N)
{ // convert N to string
String s = Integer.toString(N);
// Adding a space at the
// beginning and
// end of the string
s = ' ' + s + ' ' ;
// Traverse the string
for ( int i = 1 ; i < s.length() - 1 ; i++)
{
// If any character is not same as
// prev and next then return false
if (s.charAt(i) != s.charAt(i - 1 ) &&
s.charAt(i) != s.charAt(i + 1 ))
{
return false ;
}
}
return true ;
} // Driver code public static void main(String[] args)
{ int n = 1122 ;
if (iszygodromeNum(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
} } // This code is contributed by shubham |
# Python3 program implementation to check # if N is an zygodrome number # Function to check if N # is an zygodrome number def iszygodromeNum(N):
# Convert N to string
s = str (N);
# Adding a space at the
# beginning and
# end of the string
s = ' ' + s + ' ' ;
# Traverse the string
i = 1
while i < len (s) - 1 :
# If any character is not same as
# prev and next then return false
if ((s[i] ! = s[i - 1 ]) and
(s[i] ! = s[i + 1 ])):
return False ;
i + = 1
return True ;
# Driver code if __name__ = = '__main__' :
n = 1122 ;
if iszygodromeNum(n):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by jana_sayantan |
// C# implementation to check if N // is a zygodrome number using System;
class GFG{
// Function to check if N // is an zygodrome number static bool iszygodromeNum( int N)
{ // convert N to string
String s = N.ToString();
// Adding a space at the
// beginning and
// end of the string
s = ' ' + s + ' ' ;
// Traverse the string
for ( int i = 1; i < s.Length - 1; i++)
{
// If any character is not same as
// prev and next then return false
if (s[i] != s[i - 1] &&
s[i] != s[i + 1])
{
return false ;
}
}
return true ;
} // Driver code public static void Main(String[] args)
{ int n = 1122;
if (iszygodromeNum(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
} } // This code is contributed by Princi Singh |
<script> // Javascript implementation to check if N // is a zygodrome number // Function to check if N
// is an zygodrome number
function iszygodromeNum( N)
{
// convert N to string
let s = N.toString();
// Adding a space at the
// beginning and
// end of the string
s = ' ' + s + ' ' ;
// Traverse the string
for ( i = 1; i < s.length - 1; i++)
{
// If any letacter is not same as
// prev and next then return false
if (s[i] != s[i-1] && s[i] != s[i + 1])
{
return false ;
}
}
return true ;
}
// Driver code
let n = 1122;
if (iszygodromeNum(n))
document.write( "Yes" );
else
document.write( "No" );
// This code is contributed by todaysgaurav </script> |
Yes
Time Complexity: O(log10n)