Check if a Lexicographical Pythagorean Triplets exists in range [0, K) of lexicographically largest string
Last Updated :
14 Jan, 2022
Given a string str and a positive integer K. The task is to find if there exist a Pythagorean Triples in the first window of size K of a string which have the same characters as str but is largest in lexicographical order.
Note: Every character is in lower case and consider the following values for each alphabet to check if there exist Pythagorean triples: a = 1, b = 2, . . ., y = 25, z = 26.
A triplet(ch1, ch2, ch3) is called Pythagorean triples if (ch1)2 + (ch2)2 = (ch3)2.
Examples:
Input: str = “abxczde”, K = 4
Output: NO
Explanation: The lexicographically largest string having the same characters is zxedcba.
The first window of size 4 is “zxed”, which does not contain any such triplet.
Input: str = “abcdef”, K = 4
Output: YES
Explanation: The lexicographically largest string possible is “fedcba”.
The first window of size 4 has “fedc” which have a triplet (c, d, e) that is Pythagorean.
Input: str = “dce”, K = 2
Output: NO
Explanation: As window size is less than 3, choosing a triple is not possible.
Approach: This problem can be solved using Greedy algorithm. Follow the steps below for approach:
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool Pythagorean(string s, int k)
{
if (k < 3) {
return false ;
}
sort(s.begin(), s.end(), greater< char >());
for ( int i = 0; i < k - 2; ++i) {
int r = k - 1;
int l = i + 1;
while (r > l) {
int a = pow (s[i] - 'a' + 1, 2);
int b = pow (s[l] - 'a' + 1, 2);
int c = pow (s[r] - 'a' + 1, 2);
if (a == b + c) {
return true ;
}
else if (a > b + c) {
r--;
}
else
l++;
}
}
return false ;
}
int main()
{
string str = "abcdef" ;
int K = 4;
bool ans = Pythagorean(str, K);
if (ans)
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static void reverse( char [] a)
{
int i, n = a.length;
char t;
for (i = 0 ; i < n / 2 ; i++)
{
t = a[i];
a[i] = a[n - i - 1 ];
a[n - i - 1 ] = t;
}
}
static boolean Pythagorean(String str, int k)
{
if (k < 3 ) {
return false ;
}
char [] s = str.toCharArray();
Arrays.sort(s);
reverse(s);
for ( int i = 0 ; i < k - 2 ; ++i) {
int r = k - 1 ;
int l = i + 1 ;
while (r > l) {
int a = ( int )Math.pow(s[i] - 'a' + 1 , 2 );
int b = ( int )Math.pow(s[l] - 'a' + 1 , 2 );
int c = ( int )Math.pow(s[r] - 'a' + 1 , 2 );
if (a == b + c) {
return true ;
}
else if (a > b + c) {
r--;
}
else
l++;
}
}
return false ;
}
public static void main(String args[])
{
String str = "abcdef" ;
int K = 4 ;
boolean ans = Pythagorean(str, K);
if (ans)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def Pythagorean(st, k):
if (k < 3 ):
return False
s = []
for i in range ( len (st)):
s.append(st[i])
s.sort()
s.reverse()
for i in range (k - 2 ):
r = k - 1
l = i + 1
while (r > l):
a = pow ( ord (s[i]) - ord ( 'a' ) + 1 , 2 )
b = pow ( ord (s[l]) - ord ( 'a' ) + 1 , 2 )
c = pow ( ord (s[r]) - ord ( 'a' ) + 1 , 2 )
if (a = = b + c):
return True
elif (a > b + c) :
r - = 1
else :
l + = 1
return False
str = "abcdef"
K = 4
ans = Pythagorean( str , K)
if (ans):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG
{
static void reverse( char [] a)
{
int i, n = a.Length;
char t;
for (i = 0; i < n / 2; i++)
{
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
}
static bool Pythagorean( string str, int k)
{
if (k < 3) {
return false ;
}
char [] s = str.ToCharArray();
Array.Sort(s);
reverse(s);
for ( int i = 0; i < k - 2; ++i) {
int r = k - 1;
int l = i + 1;
while (r > l) {
int a = ( int )Math.Pow(s[i] - 'a' + 1, 2);
int b = ( int )Math.Pow(s[l] - 'a' + 1, 2);
int c = ( int )Math.Pow(s[r] - 'a' + 1, 2);
if (a == b + c) {
return true ;
}
else if (a > b + c) {
r--;
}
else
l++;
}
}
return false ;
}
public static void Main()
{
string str = "abcdef" ;
int K = 4;
bool ans = Pythagorean(str, K);
if (ans)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
<script>
function Pythagorean(st, k)
{
if (k < 3) {
return false ;
}
s = [];
for (let i = 0; i < st.length; i++) { s.push(st.charAt(i)); }
s.sort( function (a, b) { return b.charCodeAt(0) - a.charCodeAt(0) })
for (let i = 0; i < k - 2; ++i) {
let r = k - 1;
let l = i + 1;
while (r > l) {
let a = Math.pow(s[i].charCodeAt(0) - 'a' .charCodeAt(0) + 1, 2);
let b = Math.pow(s[l].charCodeAt(0) - 'a' .charCodeAt(0) + 1, 2);
let c = Math.pow(s[r].charCodeAt(0) - 'a' .charCodeAt(0) + 1, 2);
5
if (a == b + c) {
return true ;
}
else if (a > b + c) {
r--;
}
else
l++;
}
}
return false ;
}
let str = "abcdef" ;
let K = 4;
let ans = Pythagorean(str, K);
if (ans)
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(K2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...