给定一个仅包含字符x和y的字符串str ,任务是计算以x开头和结尾且至少具有y的所有子字符串。
例子:
Input: str = “xyyxx”
Output: 2
“xyyx” and “xyyxx” are the only valid sub-strings.
Input: str = “xyy”
Output: 0
方法:
- 创建一个数组countX [] ,其中countX [i]存储从i到n – 1的总数x 。
- 现在,对于字符串的每个x ,找到出现在该x之后的第一个y 。
- 并更新count = count + countX [indexOf(y)],因为使用此x作为起始索引,所有子字符串将有效,并将在找到的y之后的任何x处结束。
- 最后返回计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function that returns the index of next occurrence
// of the character c in string str starting from index start
int nextIndex(string str, int start, char c)
{
// Starting from start
for (int i = start; i < str.length(); i++) {
// If current character = c
if (str[i] == c)
return i;
}
// Not found
return -1;
}
// Function to return the count of required sub-strings
int countSubStrings(string str)
{
int i, n = str.length();
// Stores running count of 'x' starting from the end
int countX[n];
int count = 0;
for (i = n - 1; i >= 0; i--) {
if (str[i] == 'x')
count++;
countX[i] = count;
}
// Next index of 'x' starting from index 0
int nextIndexX = nextIndex(str, 0, 'x');
// Next index of 'y' starting from index 0
int nextIndexY = nextIndex(str, 0, 'y');
// To store the count of required sub-strings
count = 0;
while (nextIndexX != -1 && nextIndexY != -1) {
// If 'y' appears before 'x'
// it won't contribute to a valid sub-string
if (nextIndexX > nextIndexY) {
// Find next occurrence of 'y'
nextIndexY = nextIndex(str, nextIndexY + 1, 'y');
continue;
}
// If 'y' appears after 'x'
// every sub-string ending at an 'x' appearing after this 'y'
// and starting with the current 'x' is a valid sub-string
else {
count += countX[nextIndexY];
// Find next occurrence of 'x'
nextIndexX = nextIndex(str, nextIndexX + 1, 'x');
}
}
// Return the count
return count;
}
// Driver code
int main()
{
string s = "xyyxx";
cout << countSubStrings(s);
}
// This code is contributed by ihritik
Java
// Java implementation of the approach
public class GFG {
// Function that returns the index of next occurrence
// of the character c in string str starting from index start
static int nextIndex(String str, int start, char c)
{
// Starting from start
for (int i = start; i < str.length(); i++) {
// If current character = c
if (str.charAt(i) == c)
return i;
}
// Not found
return -1;
}
// Function to return the count of required sub-strings
static int countSubStrings(String str)
{
int i, n = str.length();
// Stores running count of 'x' starting from the end
int countX[] = new int[n];
int count = 0;
for (i = n - 1; i >= 0; i--) {
if (str.charAt(i) == 'x')
count++;
countX[i] = count;
}
// Next index of 'x' starting from index 0
int nextIndexX = nextIndex(str, 0, 'x');
// Next index of 'y' starting from index 0
int nextIndexY = nextIndex(str, 0, 'y');
// To store the count of required sub-strings
count = 0;
while (nextIndexX != -1 && nextIndexY != -1) {
// If 'y' appears before 'x'
// it won't contribute to a valid sub-string
if (nextIndexX > nextIndexY) {
// Find next occurrence of 'y'
nextIndexY = nextIndex(str, nextIndexY + 1, 'y');
continue;
}
// If 'y' appears after 'x'
// every sub-string ending at an 'x' appearing after this 'y'
// and starting with the current 'x' is a valid sub-string
else {
count += countX[nextIndexY];
// Find next occurrence of 'x'
nextIndexX = nextIndex(str, nextIndexX + 1, 'x');
}
}
// Return the count
return count;
}
// Driver code
public static void main(String[] args)
{
String s = "xyyxx";
System.out.println(countSubStrings(s));
}
}
Python3
# Python3 implementation of the approach
# Function that returns the index of next occurrence
# of the character c in string str starting from index start
def nextIndex(str, start, c):
# Starting from start
for i in range(start,len(str)):
# If current character = c
if (str[i] == c):
return i;
# Not found
return -1;
# Function to return the count of required sub-strings
def countSubStrings(str):
n = len(str)
# Stores running count of 'x' starting from the end
countX=[0]*n;
count = 0;
for i in range(n-1,-1,-1):
if (str[i] == 'x'):
count=count+1
countX[i] = count
# Next index of 'x' starting from index 0
nextIndexX = nextIndex(str, 0, 'x')
# Next index of 'y' starting from index 0
nextIndexY = nextIndex(str, 0, 'y')
# To store the count of required sub-strings
count = 0;
while (nextIndexX != -1 and nextIndexY != -1):
# If 'y' appears before 'x'
# it won't contribute to a valid sub-string
if (nextIndexX > nextIndexY):
# Find next occurrence of 'y'
nextIndexY = nextIndex(str, nextIndexY + 1, 'y')
continue
# If 'y' appears after 'x'
# every sub-string ending at an 'x' appearing after this 'y'
# and starting with the current 'x' is a valid sub-string
else :
count += countX[nextIndexY]
# Find next occurrence of 'x'
nextIndexX = nextIndex(str, nextIndexX + 1, 'x');
# Return the count
return count
# Driver code
s = "xyyxx";
print(countSubStrings(s));
# This code is contributed by ihritik
C#
// C# implementation of the approach
using System;
public class GFG {
// Function that returns the index of next occurrence
// of the character c in string str starting from index start
static int nextIndex(string str, int start, char c)
{
// Starting from start
for (int i = start; i < str.Length; i++) {
// If current character = c
if (str[i] == c)
return i;
}
// Not found
return -1;
}
// Function to return the count of required sub-strings
static int countSubStrings(string str)
{
int i, n = str.Length ;
// Stores running count of 'x' starting from the end
int []countX = new int[n];
int count = 0;
for (i = n - 1; i >= 0; i--) {
if (str[i] == 'x')
count++;
countX[i] = count;
}
// Next index of 'x' starting from index 0
int nextIndexX = nextIndex(str, 0, 'x');
// Next index of 'y' starting from index 0
int nextIndexY = nextIndex(str, 0, 'y');
// To store the count of required sub-strings
count = 0;
while (nextIndexX != -1 && nextIndexY != -1) {
// If 'y' appears before 'x'
// it won't contribute to a valid sub-string
if (nextIndexX > nextIndexY) {
// Find next occurrence of 'y'
nextIndexY = nextIndex(str, nextIndexY + 1, 'y');
continue;
}
// If 'y' appears after 'x'
// every sub-string ending at an 'x' appearing after this 'y'
// and starting with the current 'x' is a valid sub-string
else {
count += countX[nextIndexY];
// Find next occurrence of 'x'
nextIndexX = nextIndex(str, nextIndexX + 1, 'x');
}
}
// Return the count
return count;
}
// Driver code
public static void Main()
{
string s = "xyyxx";
Console.WriteLine(countSubStrings(s));
}
// This code is contributed by Ryuga
}
PHP
= 0; $i--) {
if ($str[$i] == 'x')
$count++;
$countX[$i] = $count;
}
// Next index of 'x' starting from index 0
$nextIndexX = nextIndex($str, 0, 'x');
// Next index of 'y' starting from index 0
$nextIndexY = nextIndex($str, 0, 'y');
// To store the count of required sub-strings
$count = 0;
while ($nextIndexX != -1 && $nextIndexY != -1) {
// If 'y' appears before 'x'
// it won't contribute to a valid sub-string
if ($nextIndexX > $nextIndexY) {
// Find next occurrence of 'y'
$nextIndexY = nextIndex($str, $nextIndexY + 1, 'y');
continue;
}
// If 'y' appears after 'x'
// every sub-string ending at an 'x' appearing after this 'y'
// and starting with the current 'x' is a valid sub-string
else {
$count += $countX[$nextIndexY];
// Find next occurrence of 'x'
$nextIndexX = nextIndex($str, $nextIndexX + 1, 'x');
}
}
// Return the count
return $count;
}
// Driver code
$s = "xyyxx";
echo countSubStrings($s);
?>
输出:
2
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。