检查字典上最大字符串的 [0, K) 范围内是否存在字典式毕达哥拉斯三元组
给定一个字符串str和一个正整数K 。任务是在字符串的第一个大小为 K 的窗口中查找是否存在与 str 具有相同字符但按字典顺序最大的毕达哥拉斯三元组。
注意:每个字符都是小写的,并考虑每个字母表的以下值以检查是否存在毕达哥拉斯三元组:a = 1, b = 2, . . .,y = 25,z = 26。
A triplet(ch1, ch2, ch3) is called Pythagorean triples if (ch1)2 + (ch2)2 = (ch3)2.
例子:
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.
方法:这个问题可以使用贪心算法来解决。请按照以下步骤进行处理:
- 按降序对给定的字符串进行排序。
- 在子字符串0 到 K-1中,使用两个指针方法检查是否存在任何词典毕达哥拉斯三元组。
- 如果找到任何这样的三元组,打印 Yes 并返回。否则打印编号
下面是该方法的实现:
C++
// C++ code to implement the above approach
#include
using namespace std;
// Function to check for
// Lexicographical Pythagorean triplet
bool Pythagorean(string s, int k)
{
// If k is less than 3 no triple possible
if (k < 3) {
return false;
}
// Sort the string in descending order
sort(s.begin(), s.end(), greater());
// Loop to check Pythagorean triples
for (int i = 0; i < k - 2; ++i) {
// Variables to define boundary of window
int r = k - 1;
int l = i + 1;
// Loop for using two pointer approach
// to find the other two elements
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 triple is found
if (a == b + c) {
return true;
}
// If 'a' is greater
else if (a > b + c) {
r--;
}
// If 'a' is less
else
l++;
}
}
// No such triple found
return false;
}
// Driver code
int main()
{
string str = "abcdef";
int K = 4;
bool ans = Pythagorean(str, K);
if (ans)
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java code to implement the above approach
import java.util.*;
public class GFG
{
// Utility function to reverse an array
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;
}
}
// Function to check for
// Lexicographical Pythagorean triplet
static boolean Pythagorean(String str, int k)
{
// If k is less than 3 no triple possible
if (k < 3) {
return false;
}
// Sort the string in descending order
char[] s = str.toCharArray();
Arrays.sort(s);
reverse(s);
// Loop to check Pythagorean triples
for (int i = 0; i < k - 2; ++i) {
// Variables to define boundary of window
int r = k - 1;
int l = i + 1;
// Loop for using two pointer approach
// to find the other two elements
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 triple is found
if (a == b + c) {
return true;
}
// If 'a' is greater
else if (a > b + c) {
r--;
}
// If 'a' is less
else
l++;
}
}
// No such triple found
return false;
}
// Driver code
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");
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python Program to implement
# the above approach
# Function to check for
# Lexicographical Pythagorean triplet
def Pythagorean(st, k):
# If k is less than 3 no triple possible
if (k < 3):
return False
# Sort the string in descending order
s = []
for i in range(len(st)):
s.append(st[i])
s.sort()
s.reverse()
# Loop to check Pythagorean triples
for i in range(k - 2):
# Variables to define boundary of window
r = k - 1
l = i + 1
# Loop for using two pointer approach
# to find the other two elements
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 triple is found
if (a == b + c):
return True
# If 'a' is greater
elif (a > b + c) :
r -= 1
# If 'a' is less
else:
l += 1
# No such triple found
return False
# Driver code
str = "abcdef"
K = 4
ans = Pythagorean(str, K)
if (ans):
print("YES")
else:
print("NO")
# This code is contributed by gfgking
C#
// C# code to implement the above approach
using System;
public class GFG
{
// Utility function to reverse an array
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;
}
}
// Function to check for
// Lexicographical Pythagorean triplet
static bool Pythagorean(string str, int k)
{
// If k is less than 3 no triple possible
if (k < 3) {
return false;
}
// Sort the string in descending order
char[] s = str.ToCharArray();
Array.Sort(s);
reverse(s);
// Loop to check Pythagorean triples
for (int i = 0; i < k - 2; ++i) {
// Variables to define boundary of window
int r = k - 1;
int l = i + 1;
// Loop for using two pointer approach
// to find the other two elements
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 triple is found
if (a == b + c) {
return true;
}
// If 'a' is greater
else if (a > b + c) {
r--;
}
// If 'a' is less
else
l++;
}
}
// No such triple found
return false;
}
// Driver code
public static void Main()
{
string str = "abcdef";
int K = 4;
bool ans = Pythagorean(str, K);
if (ans)
Console.Write("YES");
else
Console.Write("NO");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
YES
时间复杂度: O(K 2 )
辅助空间: O(1)