Dado un número entero N , la tarea es comprobar si N es un número de zigódrome.
El número Zygodrome es un número si está hecho de series no triviales de dígitos idénticos.
Por ejemplo: 112233, 7777333 y 1100 son todos zigodromos en base 10.
Ejemplos:
Entrada: N = 1122
Salida: Sí
Entrada: N = 26
Salida: No
Enfoque: la idea es convertir el número en una string y devolver falso si algún carácter no es igual al carácter anterior y al carácter siguiente. Dado que el primer carácter no tiene un carácter anterior y el último carácter no tiene el siguiente carácter, agregaremos un espacio al principio y al final de la string.
A continuación se muestra la implementación del enfoque anterior:
C++
// 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
// 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
# 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#
// 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
Javascript
<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>
Producción:
Yes
Complejidad de tiempo: O (log 10 n)