Zygodrome Number - GeeksforGeeks

12 Apr, 2022
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 not 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++
Java
Python3
C#
Javascript
// 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 numberbool 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 codeint main(){    int n = 1122;    if (iszygodromeNum(n))        cout << "Yes";    else        cout << "No";    return 0;}
// Java implementation to check if N// is a zygodrome numberclass GFG{ // Function to check if N// is an zygodrome numberstatic 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 codepublic 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 numberdef 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 codeif __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 numberusing System;class GFG{ // Function to check if N// is an zygodrome numberstatic 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 codepublic 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)
shubham prakash 1
princi singh
jana_sayantan
todaysgaurav
souravmahato348
rkbhola5
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Program to multiply two matrices
Fizz Buzz Implementation
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair