检查字符串的两半是否具有相同的字符集
仅给定一个小写字符的字符串,任务是检查是否可以从中间拆分一个字符串,这将产生具有相同字符和每个字符频率相同的两半。如果给定字符串的长度是奇数,则忽略中间元素并检查其余元素。
例子:
Input: abbaab
Output: NO
The two halves contain the same characters
but their frequencies do not match so they
are NOT CORRECT
Input : abccab
Output : YES
算法:
- 声明两个计数器数组,用于保存字符串,每个大小为 26。
- 现在运行一个循环并获取两个变量 i 和 j,其中 i 从 0 开始,j 从(字符串长度 - 1)开始。
- 对于字符串中的每个字符,转到计数器数组中的相应索引,并将值增加 1,并增加 i 和减少 j。这样做直到 i 小于 j。
- 完成第 3 步后,再次运行循环并比较计数器数组的值。如果第一个数组的值不等于第二个数组的值,则返回 false。
- 如果所有计数都匹配,则返回 true。
下面是上述想法的实现:
C++
// C++ program to check if it is
// possible to split string or not
#include
using namespace std;
const int MAX_CHAR = 26;
// function to check if we can split
// string or not
bool checkCorrectOrNot(string s)
{
// Counter array initialized with 0
int count1[MAX_CHAR] = {0};
int count2[MAX_CHAR] = {0};
// Length of the string
int n = s.length();
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i=0,j=n-1; i
Java
// Java program to check if it two
// half of string contain same Character
// set or not
public class GFG {
static final int MAX_CHAR = 26;
// function to check both halves
// for equality
static boolean checkCorrectOrNot(String s)
{
// Counter array initialized with 0
int[] count1 = new int[MAX_CHAR];
int[] count2 = new int[MAX_CHAR];
// Length of the string
int n = s.length();
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i = 0, j = n - 1; i < j; i++, j--)
{
// First half
count1[s.charAt(i) - 'a']++;
// Second half
count2[s.charAt(j) - 'a']++;
}
// Checking if values are different
// set flag to 1
for (int i = 0; i < MAX_CHAR; i++)
if (count1[i] != count2[i])
return false;
return true;
}
// Driver program to test above function
public static void main(String args[])
{
// String to be checked
String s = "abab";
if (checkCorrectOrNot(s))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to check if it is
# possible to split string or not
MAX_CHAR = 26
# Function to check if we
# can split string or not
def checkCorrectOrNot(s):
global MAX_CHAR
# Counter array initialized with 0
count1 = [0] * MAX_CHAR
count2 = [0] * MAX_CHAR
# Length of the string
n = len(s)
if n == 1:
return true
# Traverse till the middle
# element is reached
i = 0; j = n - 1
while (i < j):
# First half
count1[ord(s[i]) - ord('a')] += 1
# Second half
count2[ord(s[j]) - ord('a')] += 1
i += 1; j -= 1
# Checking if values are
# different set flag to 1
for i in range(MAX_CHAR):
if count1[i] != count2[i]:
return False
return True
# Driver Code
# String to be checked
s = "ababc"
print("Yes" if checkCorrectOrNot(s) else "No")
# This code is contributed by Ansu Kumari.
C#
// C# program to check if it two half of
// string contain same Character set or not
using System;
class GFG {
static int MAX_CHAR = 26;
// function to check both halves for
// equality
static bool checkCorrectOrNot(string s)
{
// Counter array initialized with 0
int []count1 = new int[MAX_CHAR];
int []count2 = new int[MAX_CHAR];
// Length of the string
int n = s.Length;
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i = 0, j = n - 1; i < j;
i++, j--)
{
// First half
count1[s[i] - 'a']++;
// Second half
count2[s[j] - 'a']++;
}
// Checking if values are different
// set flag to 1
for (int i = 0; i < MAX_CHAR; i++)
if (count1[i] != count2[i])
return false;
return true;
}
// Driver program to test above function
public static void Main()
{
// String to be checked
string s = "abab";
if (checkCorrectOrNot(s))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by nitin mittal
PHP
Javascript
C++
// C++ program to check if it is
// possible to split string or not
#include
using namespace std;
const int MAX_CHAR = 26;
// function to check if we can split
// string or not
bool checkCorrectOrNot(string s)
{
// Counter array initialized with 0
int count[MAX_CHAR] = {0};
// Length of the string
int n = s.length();
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i=0,j=n-1; i
Java
// Java program to check if it two
// half of string contain same Character
// set or not
public class GFG {
static final int MAX_CHAR = 26;
// function to check both halves
// for equality
static boolean checkCorrectOrNot(String s)
{
// Counter array initialized with 0
int[] count = new int[MAX_CHAR];
// Length of the string
int n = s.length();
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i = 0,j = n - 1; i < j; i++, j--)
{
// First half
count[s.charAt(i) - 'a']++;
// Second half
count[s.charAt(j) - 'a']--;
}
// Checking if values are different
// set flag to 1
for (int i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false;
return true;
}
// Driver program to test above function
public static void main(String args[])
{
// String to be checked
String s = "abab";
if (checkCorrectOrNot(s))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to check if it is
# possible to split string or not
MAX_CHAR = 26
# Function to check if we
# can split string or not
def checkCorrectOrNot(s):
global MAX_CHAR
# Counter array initialized with 0
count = [0] * MAX_CHAR
# Length of the string
n = len(s)
if n == 1:
return true
# Traverse till the middle
# element is reached
i = 0; j = n-1
while i < j:
# First half
count[ord(s[i]) - ord('a')] += 1
# Second half
count[ord(s[j])-ord('a')] -= 1
i += 1; j -= 1
# Checking if values are
# different, set flag to 1
for i in range(MAX_CHAR):
if count[i] != 0:
return False
return True
# Driver Code
# String to be checked
s = "abab"
print("Yes" if checkCorrectOrNot(s) else "No")
# This code is contributed by Ansu Kumari.
C#
// C# program to check if it two
// half of string contain same Character
// set or not
using System;
public class GFG {
static int MAX_CHAR = 26;
// function to check both halves
// for equality
static bool checkCorrectOrNot(String s)
{
// Counter array initialized with 0
int[] count = new int[MAX_CHAR];
// Length of the string
int n = s.Length;
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i = 0, j = n - 1; i < j; i++, j--)
{
// First half
count[s[i] - 'a']++;
// Second half
count[s[j] - 'a']--;
}
// Checking if values are different
// set flag to 1
for (int i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false;
return true;
}
// Driver program to test above function
public static void Main(String []args)
{
// String to be checked
String s = "abab";
if (checkCorrectOrNot(s))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by parashar.
PHP
Javascript
输出 :
YES
空间优化解决方案:
下面是上述方法的空间优化解决方案。
- 我们可以通过只使用 1 个计数器数组来解决这个问题。
- 取一个字符串并增加前半部分的计数,然后减少后半部分的计数。
- 如果最终计数器数组为 0,则返回 true,否则返回 False。
下面是上述想法的实现:
C++
// C++ program to check if it is
// possible to split string or not
#include
using namespace std;
const int MAX_CHAR = 26;
// function to check if we can split
// string or not
bool checkCorrectOrNot(string s)
{
// Counter array initialized with 0
int count[MAX_CHAR] = {0};
// Length of the string
int n = s.length();
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i=0,j=n-1; i
Java
// Java program to check if it two
// half of string contain same Character
// set or not
public class GFG {
static final int MAX_CHAR = 26;
// function to check both halves
// for equality
static boolean checkCorrectOrNot(String s)
{
// Counter array initialized with 0
int[] count = new int[MAX_CHAR];
// Length of the string
int n = s.length();
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i = 0,j = n - 1; i < j; i++, j--)
{
// First half
count[s.charAt(i) - 'a']++;
// Second half
count[s.charAt(j) - 'a']--;
}
// Checking if values are different
// set flag to 1
for (int i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false;
return true;
}
// Driver program to test above function
public static void main(String args[])
{
// String to be checked
String s = "abab";
if (checkCorrectOrNot(s))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to check if it is
# possible to split string or not
MAX_CHAR = 26
# Function to check if we
# can split string or not
def checkCorrectOrNot(s):
global MAX_CHAR
# Counter array initialized with 0
count = [0] * MAX_CHAR
# Length of the string
n = len(s)
if n == 1:
return true
# Traverse till the middle
# element is reached
i = 0; j = n-1
while i < j:
# First half
count[ord(s[i]) - ord('a')] += 1
# Second half
count[ord(s[j])-ord('a')] -= 1
i += 1; j -= 1
# Checking if values are
# different, set flag to 1
for i in range(MAX_CHAR):
if count[i] != 0:
return False
return True
# Driver Code
# String to be checked
s = "abab"
print("Yes" if checkCorrectOrNot(s) else "No")
# This code is contributed by Ansu Kumari.
C#
// C# program to check if it two
// half of string contain same Character
// set or not
using System;
public class GFG {
static int MAX_CHAR = 26;
// function to check both halves
// for equality
static bool checkCorrectOrNot(String s)
{
// Counter array initialized with 0
int[] count = new int[MAX_CHAR];
// Length of the string
int n = s.Length;
if (n == 1)
return true;
// traverse till the middle element
// is reached
for (int i = 0, j = n - 1; i < j; i++, j--)
{
// First half
count[s[i] - 'a']++;
// Second half
count[s[j] - 'a']--;
}
// Checking if values are different
// set flag to 1
for (int i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false;
return true;
}
// Driver program to test above function
public static void Main(String []args)
{
// String to be checked
String s = "abab";
if (checkCorrectOrNot(s))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by parashar.
PHP
Javascript
输出:
YES