Dada la string str que consta de caracteres en minúsculas, la tarea es verificar si la string es divisible por 6 después de cambiarla de acuerdo con las reglas dadas:
- ‘a’ se cambia a 1 .
- ‘b’ se cambia a 2 …
- y de manera similar, ‘z’ se cambia a 26 .
Por ejemplo, la string «abz» se cambiará a 1226 .
Ejemplo:
Entrada: str = «ab»
Salida: Sí ,
«ab» es equivalente a 12, que es divisible por 6.Entrada: str = “abc”
Salida: No
123 no es divisible por 6.
Enfoque: Se puede resolver usando un simple truco matemático que un número es divisible por 6 solo si la suma de todos sus dígitos es divisible por 3 y el último dígito del número es divisible por 2 . Encuentre la suma de los dígitos del número formado y guárdelo en una suma variable . Además, encuentre el último dígito del número y guárdelo en lastDigit .
Ahora, si la suma es divisible por 3 y el último dígito es divisible por 2 , imprima «Sí» , de lo contrario, imprima «No» .
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 to return the sum // of the digits of n int sumDigits(int n) { int sum = 0; while (n > 0) { int digit = n % 10; sum += digit; n /= 10; } return sum; } // Function that return true if the // decoded string is divisible by 6 bool isDivBySix(string str, int n) { // To store the sum of the digits int sum = 0; // For each character, get the // sum of the digits for (int i = 0; i < n; i++) { sum += (int)(str[i] - 'a' + 1); } // If the sum of digits is // not divisible by 3 if (sum % 3 != 0) return false; // Get the last digit of // the number formed int lastDigit = ((int)(str[n - 1] - 'a' + 1)) % 10; // If the last digit is // not divisible by 2 if (lastDigit % 2 != 0) return false; return true; } // Driver code int main() { string str = "ab"; int n = str.length(); if (isDivBySix(str, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the sum // of the digits of n static int sumDigits(int n) { int sum = 0; while (n > 0) { int digit = n % 10; sum += digit; n /= 10; } return sum; } // Function that return true if the // decoded string is divisible by 6 static boolean isDivBySix(String str, int n) { // To store the sum of the digits int sum = 0; // For each character, get the // sum of the digits for (int i = 0; i < n; i++) { sum += (int)(str.charAt(i) - 'a' + 1); } // If the sum of digits is // not divisible by 3 if (sum % 3 != 0) return false; // Get the last digit of // the number formed int lastDigit = ((int)(str.charAt(n - 1) - 'a' + 1)) % 10; // If the last digit is // not divisible by 2 if (lastDigit % 2 != 0) return false; return true; } // Driver code public static void main(String []args) { String str = "ab"; int n = str.length(); if (isDivBySix(str, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach # Function to return the sum # of the digits of n def sumDigits(n) : sum = 0; while (n > 0) : digit = n % 10; sum += digit; n //= 10; return sum; # Function that return true if the # decoded string is divisible by 6 def isDivBySix(string , n) : # To store the sum of the digits sum = 0; # For each character, get the # sum of the digits for i in range(n) : sum += (ord(string[i]) - ord('a') + 1); # If the sum of digits is # not divisible by 3 if (sum % 3 != 0) : return False; # Get the last digit of # the number formed lastDigit = (ord(string[n - 1]) - ord('a') + 1) % 10; # If the last digit is # not divisible by 2 if (lastDigit % 2 != 0) : return False; return True; # Driver code if __name__ == "__main__" : string = "ab"; n = len(string); if (isDivBySix(string, n)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to return the sum // of the digits of n static int sumDigits(int n) { int sum = 0; while (n > 0) { int digit = n % 10; sum += digit; n /= 10; } return sum; } // Function that return true if the // decoded string is divisible by 6 static bool isDivBySix(String str, int n) { // To store the sum of the digits int sum = 0; // For each character, get the // sum of the digits for (int i = 0; i < n; i++) { sum += (int)(str[i] - 'a' + 1); } // If the sum of digits is // not divisible by 3 if (sum % 3 != 0) return false; // Get the last digit of // the number formed int lastDigit = ((int)(str[n - 1] - 'a' + 1)) % 10; // If the last digit is // not divisible by 2 if (lastDigit % 2 != 0) return false; return true; } // Driver code public static void Main(String []args) { String str = "ab"; int n = str.Length; if (isDivBySix(str, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function to return the sum // of the digits of n function sumDigits(n) { var sum = 0; while (n > 0) { var digit = n % 10; sum += digit; n = parseInt(n/10); } return sum; } // Function that return true if the // decoded string is divisible by 6 function isDivBySix(str, n) { // To store the sum of the digits var sum = 0; // For each character, get the // sum of the digits for (var i = 0; i < n; i++) { sum += (str[i].charCodeAt(0) - 'a'.charCodeAt(0) + 1); } // If the sum of digits is // not divisible by 3 if (sum % 3 != 0) return false; // Get the last digit of // the number formed var lastDigit = ((str[n - 1].charCodeAt(0) - 'a'.charCodeAt(0) + 1)) % 10; // If the last digit is // not divisible by 2 if (lastDigit % 2 != 0) return false; return true; } // Driver code var str = "ab"; var n = str.length; if (isDivBySix(str, n)) document.write( "Yes"); else document.write( "No"); </script>
Yes
Complejidad de tiempo : O(N)
Espacio Auxiliar: O(1)