Dada una string str que representa un número, la tarea es encontrar si el número es válido o no si está al revés, es decir, al revés.
Ejemplos:
Entrada: str = “1183”
Salida: Sí
al revés (1183) = 1183
Entrada: str = “983”
Salida: No
Enfoque: Solo los dígitos 1 , 3 y 8 son los dígitos que pueden formar otro dígito válido cuando se les da la vuelta. Si el número contiene un dígito diferente a estos, imprima No ; de lo contrario, imprima Sí .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if // str is Topsy Turvy bool topsyTurvy(string str) { // For every character of the string for (int i = 0; i < str.length(); i++) { // If the current digit cannot form a // valid digit when turned upside-down if (str[i] == '2' || str[i] == '4' || str[i] == '5' || str[i] == '6' || str[i] == '7' || str[i] == '9') { return false; } } return true; } // Driver code int main() { string str = "1234"; if (topsyTurvy(str)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if // str is Topsy Turvy static boolean topsyTurvy(char[] str) { // For every character of the string for (int i = 0; i < str.length; i++) { // If the current digit cannot form a // valid digit when turned upside-down if (str[i] == '2' || str[i] == '4' || str[i] == '5' || str[i] == '6' || str[i] == '7' || str[i] == '9') { return false; } } return true; } // Driver code public static void main(String[] args) { String str = "1234"; if (topsyTurvy(str.toCharArray())) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach # Function that returns true if # str is Topsy Turvy def topsyTurvy(string) : # For every character of the string for i in range(len(string)) : # If the current digit cannot form a # valid digit when turned upside-down if (string[i] == '2' or string[i] == '4' or string[i] == '5' or string[i] == '6' or string[i] == '7' or string[i] == '9') : return False; return True; # Driver code if __name__ == "__main__" : string = "1234"; if (topsyTurvy(string)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if // str is Topsy Turvy static bool topsyTurvy(char[] str) { // For every character of the string for (int i = 0; i < str.Length; i++) { // If the current digit cannot form a // valid digit when turned upside-down if (str[i] == '2' || str[i] == '4' || str[i] == '5' || str[i] == '6' || str[i] == '7' || str[i] == '9') { return false; } } return true; } // Driver code public static void Main(String[] args) { String str = "1234"; if (topsyTurvy(str.ToCharArray())) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function that returns true if // str is Topsy Turvy function topsyTurvy( str) { // For every character of the string for (var i = 0; i < str.length; i++) { // If the current digit cannot form a // valid digit when turned upside-down if (str[i] == '2' || str[i] == '4' || str[i] == '5' || str[i] == '6' || str[i] == '7' || str[i] == '9') { return false; } } return true; } // Driver code var str = "1234"; if (topsyTurvy(str)) document.write( "Yes"); else document.write( "No"); </script>
Producción:
No
Complejidad de tiempo: O(|str|)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Naman_Garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA