Perfect Square String Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a String str and the task is to check sum of ASCII value of all characters is a perfect square or not. Examples : Input : dddddddddddddddddddddddddOutput : Yes Input : GeeksForGeeksOutput : No Recommended PracticePerfect Square StringTry It! Algorithm Calculate the string lengthCalculate sum of ASCII value of all charactersTake the square root of the number sum and store it into variable squareRootTake floor value of the squareRoot and subtract from squareRootIf the difference of floor value of squareRoot and squareRoot is 0 then print "Yes" otherwise "No" Below is the implementation of the above approach : C++ // C++ program to find if string is a // perfect square or not. #include <bits/stdc++.h> using namespace std; bool isPerfectSquareString(string str) { int sum = 0; // calculating the length of // the string int len = str.length(); // calculating the ASCII value // of the string for (int i = 0; i < len; i++) sum += (int)str[i]; // Find floating point value of // square root of x. long double squareRoot = sqrt(sum); // If square root is an integer return ((squareRoot - floor(squareRoot)) == 0); } // Driver code int main() { string str = "d"; if (isPerfectSquareString(str)) cout << "Yes"; else cout << "No"; } Java // Java program to find if string // is a perfect square or not. import java.io.*; class GFG { static boolean isPerfectSquareString(String str) { int sum = 0; // calculating the length // of the string int len = str.length(); // calculating the ASCII // value of the string for (int i = 0; i < len; i++) sum += (int)str.charAt(i); // Find floating point value // of square root of x. long squareRoot = (long)Math.sqrt(sum); // If square root is an integer return ((squareRoot - Math.floor(squareRoot)) == 0); } // Driver code public static void main (String[] args) { String str = "d"; if (isPerfectSquareString(str)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Ajit. Python3 # Python3 program to find # if string is a perfect # square or not. import math; def isPerfectSquareString(str): sum = 0; # calculating the length # of the string l = len(str); # calculating the ASCII # value of the string for i in range(l): sum = sum + ord(str[i]); # Find floating point value # of square root of x. squareRoot = math.sqrt(sum); # If square root is an integer return ((squareRoot - math.floor(squareRoot)) == 0); # Driver code str = "d"; if (isPerfectSquareString(str)): print("Yes"); else: print("No"); # This code is contributed by mits C# // C# program to find if string // is a perfect square or not. using System; class GFG { static bool isPerfectSquareString(string str) { int sum = 0; // calculating the length // of the string int len = str.Length; // calculating the ASCII // value of the string for (int i = 0; i < len; i++) sum += (int)str[i]; // Find floating point value // of square root of x. double squareRoot = Math.Sqrt(sum); double F = Math.Floor(squareRoot); // If square root is an integer return ((squareRoot - F) == 0); } // Driver Code public static void Main() { string str = "d"; if (isPerfectSquareString(str)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Sam007 PHP <?php // PHP program to find if string // is a perfect square or not. function isPerfectSquareString($str) { $sum = 0; // calculating the length // of the string $len = strlen($str); // calculating the ASCII // value of the string for ($i = 0; $i < $len; $i++) $sum += (int)$str[$i]; // Find floating point value // of square root of x. $squareRoot = sqrt($sum); // If square root is an integer return (($squareRoot - floor($squareRoot)) == 0); } // Driver code $str = "d"; if (isPerfectSquareString($str)) echo "Yes"; else echo "No"; // This code is contributed by m_kit ?> JavaScript <script> // JavaScript program to find if string is a // perfect square or not. function isPerfectSquareString(str) { var sum = 0; // Calculating the length of // the string var len = str.length; // Calculating the ASCII value // of the string for(var i = 0; i < len; i++) sum += str.charCodeAt(i); // Find floating point value of // square root of x. var squareRoot = Math.sqrt(sum); // If square root is an integer return squareRoot - Math.floor(squareRoot) == 0; } // Driver code var str = "d"; if (isPerfectSquareString(str)) document.write("Yes"); else document.write("No"); // This code is contributed by rdtank </script> OutputYes Time Complexity: O(len), where the len is the length of the string.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Perfect Square String A ajay0007 Follow Improve Article Tags : Strings Mathematical DSA Basic Coding Problems maths-perfect-square +1 More Practice Tags : MathematicalStrings Similar Reads Perfect Cube String Given 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: YesASCII value of l = 108Therefore, sum of ASCII values = 108 + 108 = 216which is a perfect cube 6 (6 * 6 * 6 = 216) Input: str = "a"Output: N 4 min read Shortest Superstring Problem Given a set of n strings arr[], find the smallest string that contains each string in the given set as substring. We may assume that no string in arr[] is substring of another string.Examples: Input: arr[] = {"geeks", "quiz", "for"}Output: geeksquizforExplanation: "geeksquizfor" contains all the thr 15+ min read Practice questions on Strings String is an important topic from GATE exam point of view. We will discuss key points on strings as well different types of questions based on that. There are two ways to store strings as character array (char p[20]) or a pointer pointing to a string (char* s = âstringâ), both of which can be access 4 min read A Binary String Game Given a Binary String S. The task is to determine the winner of the game when two players play a game optimally with the string as per the given conditions: Player 1 always starts first.Two players take turns choosing a whole block of consecutive equal characters and deleting them from a given binar 9 min read K distant string Given a string of length n and a non-negative integer k. Find k distant string of given string. Distance between two letters is the difference between their positions in the alphabet. for example: dist(c, e) = dist(e, c) = 2.dist(a, z) = dist(z, a) = 25. By using this concept, the distance between t 7 min read Like