Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check whether the given decoded string is divisible by 6

  • Last Updated : 18 Nov, 2021

Given string str consisting of lowercase characters, the task is to check whether the string is divisible by 6 after changing it according to the given rules:  

  • ‘a’ gets changed to 1.
  • ‘b’ gets changed to 2
  • and similarly, ‘z’ gets changed to 26.

For example, the string “abz” will be changed to 1226.

Example: 

Input: str = “ab” 
Output: Yes 
“ab” is equivalent to 12 which is divisible by 6.



Input: str = “abc” 
Output: No 
123 is not divisible by 6. 

Approach: It can be solved by using a simple math trick that a number is divisible by 6 only if the sum of all of its digits is divisible by 3 and the last digit of the number is divisible by 2. Find the sum of the digits of the formed number and store it in a variable sum. Also, find the last digit of the number and store it in lastDigit
Now, if the sum is divisible by 3 and the lastDigit is divisible by 2 then print “Yes” else print “No”.

Below is the implementation of the above approach: 

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>
Output
Yes

Time Complexity: O(N)

Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!