Perfect Square String
Last Updated :
20 Feb, 2023
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 : ddddddddddddddddddddddddd
Output : Yes
Input : GeeksForGeeks
Output : No
Algorithm
- Calculate the string length
- Calculate sum of ASCII value of all characters
- Take the square root of the number sum and store it into variable squareRoot
- Take floor value of the squareRoot and subtract from squareRoot
- If 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++
#include <bits/stdc++.h>
using namespace std;
bool isPerfectSquareString(string str)
{
int sum = 0;
int len = str.length();
for ( int i = 0; i < len; i++)
sum += ( int )str[i];
long double squareRoot = sqrt (sum);
return ((squareRoot -
floor (squareRoot)) == 0);
}
int main()
{
string str = "d" ;
if (isPerfectSquareString(str))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
import java.io.*;
class GFG {
static boolean isPerfectSquareString(String str)
{
int sum = 0 ;
int len = str.length();
for ( int i = 0 ; i < len; i++)
sum += ( int )str.charAt(i);
long squareRoot = ( long )Math.sqrt(sum);
return ((squareRoot -
Math.floor(squareRoot)) == 0 );
}
public static void main (String[] args)
{
String str = "d" ;
if (isPerfectSquareString(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
import math;
def isPerfectSquareString( str ):
sum = 0 ;
l = len ( str );
for i in range (l):
sum = sum + ord ( str [i]);
squareRoot = math.sqrt( sum );
return ((squareRoot -
math.floor(squareRoot)) = = 0 );
str = "d" ;
if (isPerfectSquareString( str )):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool isPerfectSquareString( string str)
{
int sum = 0;
int len = str.Length;
for ( int i = 0; i < len; i++)
sum += ( int )str[i];
double squareRoot = Math.Sqrt(sum);
double F = Math.Floor(squareRoot);
return ((squareRoot - F) == 0);
}
public static void Main()
{
string str = "d" ;
if (isPerfectSquareString(str))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isPerfectSquareString( $str )
{
$sum = 0;
$len = strlen ( $str );
for ( $i = 0; $i < $len ; $i ++)
$sum += (int) $str [ $i ];
$squareRoot = sqrt( $sum );
return (( $squareRoot -
floor ( $squareRoot )) == 0);
}
$str = "d" ;
if (isPerfectSquareString( $str ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isPerfectSquareString(str)
{
var sum = 0;
var len = str.length;
for ( var i = 0; i < len; i++)
sum += str.charCodeAt(i);
var squareRoot = Math.sqrt(sum);
return squareRoot - Math.floor(squareRoot) == 0;
}
var str = "d" ;
if (isPerfectSquareString(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(len), where the len is the length of the string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...