给定一个包含N个小写字母的字符串S ,任务是查找S中出现的恰好为1的最小子字符串的长度。
例子:
Input: S = “abaaba”
Output: 2
Explanation:
The smallest substring in the string S, whose occurrence is exactly 1 is “aa” . Length of this substring is 2.
Therefore, print 2.
Input: S = “zyzyzyz”
Output: 5
方法:解决该问题的想法是生成给定字符串S的所有可能的子字符串,并将每个子字符串的频率存储在HashMap中。现在,遍历HashMap并打印频率为1的最小长度的子字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the smallest
// substring occurring only once
int smallestSubstring(string a)
{
// Stores all occurences
vector a1;
// Generate all the substrings
for (int i = 0; i < a.size(); i++)
{
for (int j = i + 1; j < a.size(); j++)
{
// Avoid multiple occurences
if (i != j)
// Append all substrings
a1.push_back(a.substr(i,j+1));
}
}
// Take into account
// all the substrings
map a2;
for(string i:a1) a2[i]++;
vector freshlist;
// Iterate over all
// unique substrings
for (auto i:a2)
{
// If frequency is 1
if (i.second == 1)
// Append into fresh list
freshlist.push_back(i.first);
}
// Initialize a dictionary
map dictionary;
for (auto i:freshlist)
{
// Append the keys
dictionary[i] = i.size();
}
vector newlist;
// Traverse the dictionary
for (auto i:dictionary)
newlist.push_back(i.second);
int ans = INT_MAX;
for(int i:newlist) ans = min(ans, i);
// Print the minimum of dictionary
return ans;
}
// Driver Code
int main()
{
string S = "ababaabba";
cout<
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the smallest
// substring occurring only once
static int smallestSubstring(String a)
{
// Stores all occurences
ArrayList a1 = new ArrayList<>();
// Generate all the substrings
for(int i = 0; i < a.length(); i++)
{
for(int j = i + 1; j <= a.length(); j++)
{
// Avoid multiple occurences
if (i != j)
// Append all substrings
a1.add(a.substring(i, j));
}
}
// Take into account
// all the substrings
TreeMap a2 = new TreeMap<>();
for(String s : a1)
a2.put(s, a2.getOrDefault(s, 0) + 1);
ArrayList freshlist = new ArrayList<>();
// Iterate over all
// unique substrings
for(String s : a2.keySet())
{
// If frequency is 1
if (a2.get(s) == 1)
// Append into fresh list
freshlist.add(s);
}
// Initialize a dictionary
TreeMap dictionary = new TreeMap<>();
for(String s : freshlist)
{
// Append the keys
dictionary.put(s, s.length());
}
ArrayList newlist = new ArrayList<>();
// Traverse the dictionary
for(String s : dictionary.keySet())
newlist.add(dictionary.get(s));
int ans = Integer.MAX_VALUE;
for(int i : newlist)
ans = Math.min(ans, i);
// Return the minimum of dictionary
return ans == Integer.MAX_VALUE ? 0 : ans;
}
// Driver Code
public static void main(String[] args)
{
String S = "ababaabba";
System.out.println(smallestSubstring(S));
}
}
// This code is contributed by Kingash
Python3
# Python3 program of the above approach
from collections import Counter
# Function to find the smallest
# substring occurring only once
def smallestSubstring(a):
# Stores all occurences
a1 = []
# Generate all the substrings
for i in range(len(a)):
for j in range(i+1, len(a)):
# Avoid multiple occurences
if i != j:
# Append all substrings
a1.append(a[i:j+1])
# Take into account
# all the substrings
a2 = Counter(a1)
freshlist = []
# Iterate over all
# unique substrings
for i in a2:
# If frequency is 1
if a2[i] == 1:
# Append into fresh list
freshlist.append(i)
# Initialize a dictionary
dictionary = dict()
for i in range(len(freshlist)):
# Append the keys
dictionary[freshlist[i]] = len(freshlist[i])
newlist = []
# Traverse the dictionary
for i in dictionary:
newlist.append(dictionary[i])
# Print the minimum of dictionary
return(min(newlist))
# Driver Code
S = "ababaabba"
print(smallestSubstring(S))
输出:
2
时间复杂度: O(N 2 )
辅助空间: O(N 2 )