给定一个字符串,找到字符串中的任意两个字符之间的最大字符数。如果没有字符重复,则打印 -1。
例子:
Input : str = "abba"
Output : 2
The maximum number of characters are
between two occurrences of 'a'.
Input : str = "baaabcddc"
Output : 3
The maximum number of characters are
between two occurrences of 'b'.
Input : str = "abc"
Output : -1
方法 1(简单):使用两个嵌套循环。外循环从左到右选择字符,内循环找到最远的出现并跟踪最大值。
C++
// Simple C++ program to find maximum number
// of characters between two occurrences of
// same character
#include
using namespace std;
int maximumChars(string& str)
{
int n = str.length();
int res = -1;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (str[i] == str[j])
res = max(res, abs(j - i - 1));
return res;
}
// Driver code
int main()
{
string str = "abba";
cout << maximumChars(str);
return 0;
}
Java
// Simple Java program to find maximum
// number of characters between two
// occurrences of same character
class GFG {
static int maximumChars(String str)
{
int n = str.length();
int res = -1;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (str.charAt(i) == str.charAt(j))
res = Math.max(res,
Math.abs(j - i - 1));
return res;
}
// Driver code
public static void main(String[] args)
{
String str = "abba";
System.out.println(maximumChars(str));
}
}
// This code is contributed by vt_m.
Python3
# Simple Python3 program to find maximum number
# of characters between two occurrences of
# same character
def maximumChars(str):
n = len(str)
res = -1
for i in range(0, n - 1):
for j in range(i + 1, n):
if (str[i] == str[j]):
res = max(res, abs(j - i - 1))
return res
# Driver code
if __name__ == '__main__':
str = "abba"
print(maximumChars(str))
# This code is contributed by PrinciRaj1992
C#
// Simple C# program to find maximum
// number of characters between two
// occurrences of same character
using System;
public class GFG {
static int maximumChars(string str)
{
int n = str.Length;
int res = -1;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (str[i] == str[j])
res = Math.Max(res,
Math.Abs(j - i - 1));
return res;
}
// Driver code
static public void Main ()
{
string str = "abba";
Console.WriteLine(maximumChars(str));
}
}
// This code is contributed by vt_m.
Javascript
C++
// Efficient C++ program to find maximum number
// of characters between two occurrences of
// same character
#include
using namespace std;
const int MAX_CHAR = 256;
int maximumChars(string& str)
{
int n = str.length();
int res = -1;
// Initialize all indexes as -1.
int firstInd[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
firstInd[i] = -1;
for (int i = 0; i < n; i++) {
int first_ind = firstInd[str[i]];
// If this is first occurrence
if (first_ind == -1)
firstInd[str[i]] = i;
// Else find distance from previous
// occurrence and update result (if
// required).
else
res = max(res, abs(i - first_ind - 1));
}
return res;
}
// Driver code
int main()
{
string str = "abba";
cout << maximumChars(str);
return 0;
}
Java
// Efficient java program to find maximum
// number of characters between two
// occurrences of same character
import java.io.*;
public class GFG {
static int MAX_CHAR = 256;
static int maximumChars(String str)
{
int n = str.length();
int res = -1;
// Initialize all indexes as -1.
int []firstInd = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
firstInd[i] = -1;
for (int i = 0; i < n; i++)
{
int first_ind = firstInd[str.charAt(i)];
// If this is first occurrence
if (first_ind == -1)
firstInd[str.charAt(i)] = i;
// Else find distance from previous
// occurrence and update result (if
// required).
else
res = Math.max(res, Math.abs(i
- first_ind - 1));
}
return res;
}
// Driver code
static public void main (String[] args)
{
String str = "abba";
System.out.println(maximumChars(str));
}
}
// This code is contributed by vt_m.
Python3
# Efficient Python3 program to find maximum
# number of characters between two occurrences
# of same character
MAX_CHAR = 256
def maximumChars(str1):
n = len(str1)
res = -1
# Initialize all indexes as -1.
firstInd = [-1 for i in range(MAX_CHAR)]
for i in range(n):
first_ind = firstInd[ord(str1[i])]
# If this is first occurrence
if (first_ind == -1):
firstInd[ord(str1[i])] = i
# Else find distance from previous
# occurrence and update result (if
# required).
else:
res = max(res, abs(i - first_ind - 1))
return res
# Driver code
str1 = "abba"
print(maximumChars(str1))
# This code is contributed by Mohit kumar 29
C#
// Efficient C# program to find maximum
// number of characters between two
// occurrences of same character
using System;
public class GFG {
static int MAX_CHAR = 256;
static int maximumChars(string str)
{
int n = str.Length;
int res = -1;
// Initialize all indexes as -1.
int []firstInd = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
firstInd[i] = -1;
for (int i = 0; i < n; i++)
{
int first_ind = firstInd[str[i]];
// If this is first occurrence
if (first_ind == -1)
firstInd[str[i]] = i;
// Else find distance from previous
// occurrence and update result (if
// required).
else
res = Math.Max(res, Math.Abs(i
- first_ind - 1));
}
return res;
}
// Driver code
static public void Main ()
{
string str = "abba";
Console.WriteLine(maximumChars(str));
}
}
// This code is contributed by vt_m.
Javascript
输出:
2
时间复杂度: O(n^2)
方法 2(高效):初始化一个长度为 26 的数组“FIRST”,我们必须在其中存储字符串第一次出现的字母表和另一个长度为 26 的数组“LAST”,我们将存储最后一次出现的字母表在字符串。这里,索引 0 对应于字母 a,1 对应于 b 等等。之后,如果它们不在同一位置,我们将取最后一个和第一个数组之间的差异来找到最大差异。
C++
// Efficient C++ program to find maximum number
// of characters between two occurrences of
// same character
#include
using namespace std;
const int MAX_CHAR = 256;
int maximumChars(string& str)
{
int n = str.length();
int res = -1;
// Initialize all indexes as -1.
int firstInd[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
firstInd[i] = -1;
for (int i = 0; i < n; i++) {
int first_ind = firstInd[str[i]];
// If this is first occurrence
if (first_ind == -1)
firstInd[str[i]] = i;
// Else find distance from previous
// occurrence and update result (if
// required).
else
res = max(res, abs(i - first_ind - 1));
}
return res;
}
// Driver code
int main()
{
string str = "abba";
cout << maximumChars(str);
return 0;
}
Java
// Efficient java program to find maximum
// number of characters between two
// occurrences of same character
import java.io.*;
public class GFG {
static int MAX_CHAR = 256;
static int maximumChars(String str)
{
int n = str.length();
int res = -1;
// Initialize all indexes as -1.
int []firstInd = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
firstInd[i] = -1;
for (int i = 0; i < n; i++)
{
int first_ind = firstInd[str.charAt(i)];
// If this is first occurrence
if (first_ind == -1)
firstInd[str.charAt(i)] = i;
// Else find distance from previous
// occurrence and update result (if
// required).
else
res = Math.max(res, Math.abs(i
- first_ind - 1));
}
return res;
}
// Driver code
static public void main (String[] args)
{
String str = "abba";
System.out.println(maximumChars(str));
}
}
// This code is contributed by vt_m.
蟒蛇3
# Efficient Python3 program to find maximum
# number of characters between two occurrences
# of same character
MAX_CHAR = 256
def maximumChars(str1):
n = len(str1)
res = -1
# Initialize all indexes as -1.
firstInd = [-1 for i in range(MAX_CHAR)]
for i in range(n):
first_ind = firstInd[ord(str1[i])]
# If this is first occurrence
if (first_ind == -1):
firstInd[ord(str1[i])] = i
# Else find distance from previous
# occurrence and update result (if
# required).
else:
res = max(res, abs(i - first_ind - 1))
return res
# Driver code
str1 = "abba"
print(maximumChars(str1))
# This code is contributed by Mohit kumar 29
C#
// Efficient C# program to find maximum
// number of characters between two
// occurrences of same character
using System;
public class GFG {
static int MAX_CHAR = 256;
static int maximumChars(string str)
{
int n = str.Length;
int res = -1;
// Initialize all indexes as -1.
int []firstInd = new int[MAX_CHAR];
for (int i = 0; i < MAX_CHAR; i++)
firstInd[i] = -1;
for (int i = 0; i < n; i++)
{
int first_ind = firstInd[str[i]];
// If this is first occurrence
if (first_ind == -1)
firstInd[str[i]] = i;
// Else find distance from previous
// occurrence and update result (if
// required).
else
res = Math.Max(res, Math.Abs(i
- first_ind - 1));
}
return res;
}
// Driver code
static public void Main ()
{
string str = "abba";
Console.WriteLine(maximumChars(str));
}
}
// This code is contributed by vt_m.
Javascript
输出:
2
时间复杂度: O(n)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。