给定字符串小写英文字母和一个整数 0 < K <= 26。任务是将字符串分成两部分(也打印它们),这样两部分至少有 k 个不同的字符。如果可能有多个答案,请打印左边部分最小的答案。如果没有这样的答案,打印“不可能”。
例子:
Input : str = “geeksforgeeks”, k = 4
Output : geeks , forgeeks
The string can be divided into two parts as “geeks” and “forgeeks”. Since “geeks” has four different characters ‘g’, ‘e’, ‘k’ and ‘s’ and this is the smallest left part, “forgeeks” has also at least four different characters.
Input : str = “aaaabbbb”, k = 2
Output :Not Possible
方法 :
- 想法是使用 Hashmap 计算不同字符的数量。
- 如果不同变量的计数变得等于k ,则找到字符串的左侧部分,因此存储此索引,中断循环并取消标记所有字符。
- 现在从左字符串结束到给定字符串的末尾运行一个循环,并重复与查找左字符串相同的过程。
- 如果 count 大于或等于k ,则可以找到正确的字符串,否则打印“不可能”。
- 如果可能,则打印左字符串和右字符串。
下面是上述方法的实现
C++
// C++ implementation of the above approach
#include
#include
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to find the partition of the
// string such that both parts have at
// least k different characters
static void division_of_string(char[] str, int k)
{
// Length of the string
int n = str.length;
// To check if the current
// character is already found
Map has = new HashMap<>();
int ans = 0, cnt = 0, i = 0;
// Count number of different
// characters in the left part
while (i < n)
{
// If current character is not
// already found, increase cnt by 1
if (!has.containsKey(str[i]))
{
cnt++;
has.put(str[i], true);
}
// If count becomes equal to k, we've
// got the first part, therefore,
// store current index and break the loop
if (cnt == k)
{
ans = i;
break;
}
i++;
}
// Clear the map
has.clear();
// Assign cnt as 0
cnt = 0;
while (i < n)
{
// If the current character is not
// already found, increase cnt by 1
if (!has.containsKey(str[i]))
{
cnt++;
has.put(str[i], true);
}
// If cnt becomes equal to k, the
// second part also have k different
// characters so break it
if (cnt == k)
{
break;
}
i++;
}
// If the second part has less than
// k different characters, then
// print "Not Possible"
if (cnt < k)
{
System.out.println("Not possible");
}
// Otherwise print both parts
else
{
i = 0;
while (i <= ans)
{
System.out.print(str[i]);
i++;
}
System.out.println("");
while (i < n)
{
System.out.print(str[i]);
i++;
}
System.out.println("");
}
System.out.println("");
}
// Driver code
public static void main(String[] args)
{
String str = "geeksforgeeks";
int k = 4;
// Function call
division_of_string(str.toCharArray(), k);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the above approach
# Function to find the partition of the
# string such that both parts have at
# least k different characters
def division_of_string(string, k) :
# Length of the string
n = len(string);
# To check if the current
# character is already found
has = {};
cnt = 0; i = 0;
# Count number of different
# characters in the left part
while (i < n) :
# If current character is not
# already found, increase cnt by 1
if string[i] not in has :
cnt += 1;
has[string[i]] = True;
# If count becomes equal to k, we've
# got the first part, therefore,
# store current index and break the loop
if (cnt == k) :
ans = i;
break;
i += 1;
# Clear the map
has.clear();
# Assign cnt as 0
cnt = 0;
while (i < n) :
# If the current character is not
# already found, increase cnt by 1
if (string[i] not in has) :
cnt += 1;
has[string[i]] = True;
# If cnt becomes equal to k, the
# second part also have k different
# characters so break it
if (cnt == k) :
break;
i += 1;
# If the second part has less than
# k different characters, then
# print "Not Possible"
if (cnt < k) :
print("Not possible",end = "");
# Otherwise print both parts
else :
i = 0;
while (i <= ans) :
print(string[i],end= "");
i += 1;
print();
while (i < n) :
print(string[i],end="");
i += 1;
print()
# Driver code
if __name__ == "__main__":
string = "geeksforgeeks";
k = 4;
# Function call
division_of_string(string, k);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find the partition of the
// string such that both parts have at
// least k different characters
static void division_of_string(char[] str, int k)
{
// Length of the string
int n = str.Length;
// To check if the current
// character is already found
Dictionary has = new Dictionary ();
int ans = 0, cnt = 0, i = 0;
// Count number of different
// characters in the left part
while (i < n)
{
// If current character is not
// already found, increase cnt by 1
if (!has.ContainsKey(str[i]))
{
cnt++;
has.Add(str[i], true);
}
// If count becomes equal to k, we've
// got the first part, therefore,
// store current index and break the loop
if (cnt == k)
{
ans = i;
break;
}
i++;
}
// Clear the map
has.Clear();
// Assign cnt as 0
cnt = 0;
while (i < n)
{
// If the current character is not
// already found, increase cnt by 1
if (!has.ContainsKey(str[i]))
{
cnt++;
has.Add(str[i], true);
}
// If cnt becomes equal to k, the
// second part also have k different
// characters so break it
if (cnt == k)
{
break;
}
i++;
}
// If the second part has less than
// k different characters, then
// print "Not Possible"
if (cnt < k)
{
Console.WriteLine("Not possible");
}
// Otherwise print both parts
else
{
i = 0;
while (i <= ans)
{
Console.Write(str[i]);
i++;
}
Console.WriteLine("");
while (i < n)
{
Console.Write(str[i]);
i++;
}
Console.WriteLine("");
}
Console.WriteLine("");
}
// Driver code
public static void Main(String[] args)
{
String str = "geeksforgeeks";
int k = 4;
// Function call
division_of_string(str.ToCharArray(), k);
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
geeks
forgeeks
时间复杂度: O(N) 其中 N 是给定字符串的长度。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。