Perfect Cube String

Given a String str, the task is to check the sum of ASCII values of all characters in this string is a perfect cube or not.

Examples :

Input: str = "ll"
Output: Yes
ASCII value of l = 108
Therefore, sum of ASCII values = 108 + 108 = 216
which is a perfect cube 6 (6 * 6 * 6 = 216)

Input: str = "a"
Output: No
ASCII value of a = 97
Therefore, sum of ASCII values = 97
which is not a perfect cube

Algorithm

  • For each character in the String, find out its ASCII value
  • Calculate sum of ASCII values of all characters
  • Check whether this sum is a perfect cube or not.
  • If the sum is a perfect cube, then print “Yes” otherwise “No”

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if string is a
// perfect cube or not.
  
#include <bits/stdc++.h>
using namespace std;
  
bool isPerfectCubeString(string str)
{
    int sum = 0;
  
    // Finding ASCII values of each
    // character and finding its sum
    for (int i = 0; i < str.length(); i++)
        sum += (int)str[i];
  
    // Find the cube root of sum
    long double cr = round(cbrt(sum));
  
    // Check if sum is a perfect cube
    return (cr * cr * cr == sum);
}
  
// Driver code
int main()
{
    string str = "ll";
  
    if (isPerfectCubeString(str))
        cout << "Yes";
    else
        cout << "No";
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find if String is a
// perfect cube or not.
import java.util.*;
  
class GFG{
   
static boolean isPerfectCubeString(String str)
{
    int sum = 0;
   
    // Finding ASCII values of each
    // character and finding its sum
    for (int i = 0; i < str.length(); i++)
        sum += (int)str.charAt(i);
   
    // Find the cube root of sum
    double cr = Math.round(Math.cbrt(sum));
   
    // Check if sum is a perfect cube
    return (cr * cr * cr == sum);
}
   
// Driver code
public static void main(String[] args)
{
    String str = "ll";
   
    if (isPerfectCubeString(str))
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find if str1ing is a
# perfect cube or not.
from math import ceil
  
def isPerfectCubeString(str1):
    sum = 0
  
    # Finding ASCII values of each
    # character and finding its sum
    for i in range(len(str1)):
        sum += ord(str1[i])
  
    # Find the cube root of sum
    cr = ceil((sum)**(1/3))
  
    # Check if sum is a perfect cube
    return (cr * cr * cr == sum)
  
# Driver code
str1 = "ll"
  
if (isPerfectCubeString(str1)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find if String is a
// perfect cube or not.
using System;
  
class GFG{
  
static bool isPerfectCubeString(String str)
{
    int sum = 0;
  
    // Finding ASCII values of each
    // character and finding its sum
    for (int i = 0; i < str.Length; i++)
        sum += (int)str[i];
  
    // Find the cube root of sum
    double cr = Math.Round(Math.Pow(sum, (double) 1 / 3));
      
    // Check if sum is a perfect cube
    return (cr * cr * cr == sum);
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "ll";
  
    if (isPerfectCubeString(str))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

Yes

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.