给定一个大小为N的字符串S 。最初, count 的值为0 。任务是在N 次操作后找到 count 的值,以删除给定字符串S 的所有N 个字符,其中每个操作是:
- 在每个操作中,选择一个按字母顺序排列的字符串S 中最小的字符,并从S 中删除该字符并将其索引添加到计数中。
- 如果存在多个字符,则删除索引最小的字符。
注意:将字符串视为基于 1 的索引。
例子:
Input: N = 5, S = “abcab”
Output: 8
Explanation:
Remove character ‘a’ then string becomes “bcab” and the count = 1.
Remove character ‘a’ then string becomes “bcb” and the count = 1 + 3 = 4.
Remove character ‘b’ then string becomes “cb” and the count = 4 + 1 = 5.
Remove character ‘b’ then string becomes “c” and the count = 5 + 2 = 7.
Remove character ‘c’ then string becomes “” and the count = 7 + 1 = 8.
Input: N = 5 S = “aabbc”
Output: 5
Explanation:
The value after 5 operations to remove all the 5 characters of String aabbc is 5.
天真的方法:这个想法是检查字符串是否为空。如果它不为空,那么以下是解决问题的步骤:
- 查找当前字符串最小字母的第一次出现,假设ind并将其从字符串删除。
- 将计数增加ind + 1。
- 重复上述步骤,直到字符串变为空。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to find the value after
// N operations to remove all the N
// characters of string S
void charactersCount(string str, int n)
{
int count = 0;
// Iterate till N
while (n > 0) {
char cur = str[0];
int ind = 0;
for (int j = 1; j < n; j++) {
if (str[j] < cur) {
cur = str[j];
ind = j;
}
}
// Remove character at ind and
// decrease n(size of string)
str.erase(str.begin() + ind);
n--;
// Increase count by ind+1
count += ind + 1;
}
cout << count << endl;
}
// Driver Code
int main()
{
// Given string str
string str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the value after
// N operations to remove all the N
// characters of String S
static void charactersCount(String str, int n)
{
int count = 0;
// Iterate till N
while (n > 0)
{
char cur = str.charAt(0);
int ind = 0;
for(int j = 1; j < n; j++)
{
if (str.charAt(j) < cur)
{
cur = str.charAt(j);
ind = j;
}
}
// Remove character at ind and
// decrease n(size of String)
str = str.substring(0, ind) +
str.substring(ind + 1);
n--;
// Increase count by ind+1
count += ind + 1;
}
System.out.print(count + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 program for the above approach
# Function to find the value after
# N operations to remove all the N
# characters of String S
def charactersCount(str, n):
count = 0;
# Iterate till N
while (n > 0):
cur = str[0];
ind = 0;
for j in range(1, n):
if (str[j] < cur):
cur = str[j];
ind = j;
# Remove character at ind and
# decrease n(size of String)
str = str[0 : ind] + str[ind + 1:];
n -= 1;
# Increase count by ind+1
count += ind + 1;
print(count);
# Driver Code
if __name__ == '__main__':
# Given String str
str = "aabbc";
n = 5;
# Function call
charactersCount(str, n);
# This code is contributed by Amit Katiyar
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the value after
// N operations to remove all the N
// characters of String S
static void charactersCount(String str, int n)
{
int count = 0;
// Iterate till N
while (n > 0)
{
char cur = str[0];
int ind = 0;
for(int j = 1; j < n; j++)
{
if (str[j] < cur)
{
cur = str[j];
ind = j;
}
}
// Remove character at ind and
// decrease n(size of String)
str = str.Substring(0, ind) +
str.Substring(ind + 1);
n--;
// Increase count by ind+1
count += ind + 1;
}
Console.Write(count + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
}
}
// This code is contributed by gauravrajput1
C++
// C++ program for the above approach
#include
using namespace std;
// Structure of Fenwick tree
struct FenwickTree {
// Binary indexed tree
vector bit;
// bit[0] is not involved
int n;
// Constructor
FenwickTree(int n)
{
this->n = n + 1;
bit = vector(n, 0);
}
// Constructor
FenwickTree(vector a)
: FenwickTree(a.size())
{
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
// Sum of arr[0] + arr[1] + ...
// + arr[idx] where arr is array
int sum(int idx)
{
int ret = 0;
// Index in BITree[] is 1 more
// than the index in arr[]
idx = idx + 1;
// Traverse the ancestors of
// BITree[index]
while (idx > 0) {
// Add current element
// of BITree to sum
ret += bit[idx];
// Move index to parent
// node in getSum View
idx -= idx & (-idx);
}
// Return the result
return ret;
}
// Function for adding arr[idx]
// is equals to arr[idx] + val
void add(int idx, int val)
{
// Val is nothing but "DELTA"
// index in BITree[] is 1
// more than the index in arr[]
idx = idx + 1;
// Traverse all ancestors
// and add 'val'
while (idx <= n) {
// Add 'val' to current
// node of BI Tree
bit[idx] += val;
// Update index to that
// of parent in update View
idx += idx & (-idx);
}
}
// Update the index
void update(int idx, int val)
{
add(idx, val - valat(idx));
}
// Perform the sum arr[l] + arr[l+1]
// + ... + arr[r]
int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
int valat(int i)
{
return sum(i, i);
}
void clr(int sz)
{
bit.clear();
n = sz + 1;
bit.resize(n + 1, 0);
// Initially mark all
// are present (i.e. 1)
for (int i = 0; i < n; i++)
add(i, 1);
}
};
// Function to count the characters
void charactersCount(string str, int n)
{
int i, count = 0;
// Store the values of indexes
// for each alphabet
vector > mp
= vector >(26,
vector());
// Create FenwickTree of size n
FenwickTree ft = FenwickTree(n);
ft.clr(n);
// Initially no indexes are
// stored for each character
for (i = 0; i < 26; i++)
mp[i].clear();
i = 0;
for (char ch : str) {
// Push 'i' to mp[ch]
mp[ch - 'a'].push_back(i);
i++;
}
// Start with smallest character
// and move towards larger character
// i.e., from 'a' to 'z' (0 to 25)
for (i = 0; i < 26; i++) {
// index(ind) of current character
// corres to i are obtained
// in increasing order
for (int ind : mp[i]) {
// Find the number of characters
// currently present before
// ind using ft.sum(ind)
count += ft.sum(ind);
// Mark the corresponding
// ind as removed and
// make value at ind as 0
ft.update(ind, 0);
}
}
// Print the value of count
cout << count << endl;
}
// Driver Code
int main()
{
// Given string str
string str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Structure of Fenwick tree
static class FenwickTree
{
// Binary indexed tree
int []bit;
// bit[0] is not involved
int n;
// Constructor
FenwickTree(int n)
{
this.n = n + 1;
bit = new int[n];
}
// Constructor
FenwickTree(Vector a)
{
for(int i = 0; i < a.size(); i++)
add(i, a.get(i));
}
// Sum of arr[0] + arr[1] + ...
// + arr[idx] where arr is array
int sum(int idx)
{
int ret = 0;
// Index in BITree[] is 1 more
// than the index in arr[]
idx = idx + 1;
// Traverse the ancestors of
// BITree[index]
while (idx > 0)
{
// Add current element
// of BITree to sum
ret += bit[idx];
// Move index to parent
// node in getSum View
idx -= idx & (-idx);
}
// Return the result
return ret;
}
// Function for adding arr[idx]
// is equals to arr[idx] + val
void add(int idx, int val)
{
// Val is nothing but "DELTA"
// index in BITree[] is 1
// more than the index in arr[]
idx = idx + 1;
// Traverse all ancestors
// and add 'val'
while (idx <= n)
{
// Add 'val' to current
// node of BI Tree
bit[idx] += val;
// Update index to that
// of parent in update View
idx += idx & (-idx);
}
}
// Update the index
void update(int idx, int val)
{
add(idx, val - valat(idx));
}
// Perform the sum arr[l] + arr[l+1]
// + ... + arr[r]
int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
int valat(int i)
{
return sum(i, i);
}
void clr(int sz)
{
n = sz + 1;
bit = new int[n + 1];
// Initially mark all
// are present (i.e. 1)
for(int i = 0; i < n; i++)
add(i, 1);
}
};
// Function to count the characters
static void charactersCount(String str, int n)
{
int i, count = 0;
// Store the values of indexes
// for each alphabet
@SuppressWarnings("unchecked")
Vector []mp = new Vector[26];
for(i = 0; i < mp.length; i++)
mp[i] = new Vector();
// Create FenwickTree of size n
FenwickTree ft = new FenwickTree(n);
ft.clr(n);
// Initially no indexes are
// stored for each character
for(i = 0; i < 26; i++)
mp[i].clear();
i = 0;
for(char ch : str.toCharArray())
{
// Push 'i' to mp[ch]
mp[ch - 'a'].add(i);
i++;
}
// Start with smallest character
// and move towards larger character
// i.e., from 'a' to 'z' (0 to 25)
for(i = 0; i < 26; i++)
{
// index(ind) of current character
// corres to i are obtained
// in increasing order
for(int ind : mp[i])
{
// Find the number of characters
// currently present before
// ind using ft.sum(ind)
count += ft.sum(ind);
// Mark the corresponding
// ind as removed and
// make value at ind as 0
ft.update(ind, 0);
}
}
// Print the value of count
System.out.print(count + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Structure of Fenwick tree
public class FenwickTree
{
// Binary indexed tree
public int []bit;
// bit[0] is not involved
int n;
// Constructor
public FenwickTree(int n)
{
this.n = n + 1;
bit = new int[n];
}
// Constructor
public FenwickTree(List a)
{
for(int i = 0; i < a.Count; i++)
add(i, a[i]);
}
// Sum of arr[0] + arr[1] + ...
// + arr[idx] where arr is array
public int sum(int idx)
{
int ret = 0;
// Index in BITree[] is 1 more
// than the index in []arr
idx = idx + 1;
// Traverse the ancestors of
// BITree[index]
while (idx > 0)
{
// Add current element
// of BITree to sum
ret += bit[idx];
// Move index to parent
// node in getSum View
idx -= idx & (-idx);
}
// Return the result
return ret;
}
// Function for adding arr[idx]
// is equals to arr[idx] + val
public void add(int idx, int val)
{
// Val is nothing but "DELTA"
// index in BITree[] is 1
// more than the index in []arr
idx = idx + 1;
// Traverse all ancestors
// and add 'val'
while (idx <= n)
{
// Add 'val' to current
// node of BI Tree
bit[idx] += val;
// Update index to that
// of parent in update View
idx += idx & (-idx);
}
}
// Update the index
public void update(int idx, int val)
{
add(idx, val - valat(idx));
}
// Perform the sum arr[l] + arr[l+1]
// + ... + arr[r]
public int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
public int valat(int i)
{
return sum(i, i);
}
public void clr(int sz)
{
n = sz + 1;
bit = new int[n + 1];
// Initially mark all
// are present (i.e. 1)
for(int i = 0; i < n; i++)
add(i, 1);
}
};
// Function to count the characters
static void charactersCount(String str, int n)
{
int i, count = 0;
// Store the values of indexes
// for each alphabate
List []mp = new List[26];
for(i = 0; i < mp.Length; i++)
mp[i] = new List();
// Create FenwickTree of size n
FenwickTree ft = new FenwickTree(n);
ft.clr(n);
// Initially no indexes are
// stored for each character
for(i = 0; i < 26; i++)
mp[i].Clear();
i = 0;
foreach(char ch in str.ToCharArray())
{
// Push 'i' to mp[ch]
mp[ch - 'a'].Add(i);
i++;
}
// Start with smallest character
// and move towards larger chracter
// i.e., from 'a' to 'z' (0 to 25)
for(i = 0; i < 26; i++)
{
// index(ind) of current character
// corres to i are obtained
// in increasing order
foreach(int ind in mp[i])
{
// Find the number of characters
// currently present before
// ind using ft.sum(ind)
count += ft.sum(ind);
// Mark the corresponding
// ind as removed and
// make value at ind as 0
ft.update(ind, 0);
}
}
// Print the value of count
Console.Write(count + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
}
}
// This code is contributed by amal kumar choubey
5
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:这个问题可以使用二叉索引树或芬威克树的概念来解决。以下是步骤:
- 最初,按升序存储所有字符/alphabet 的索引值。
- 从最小的字母“a”开始,并按照出现的顺序删除所有“a” 。删除后,选择下一个字母‘b’ ,并重复此步骤,直到删除所有字母。
- 去掉字符意味着它在数组中对应的值变成了0 ,表示被去掉了。
- 在移除之前,使用Fenwick Tree 中的sum()方法找到字符串字符的位置,并将位置值添加到计数中。
- 去掉字符串中的所有字符后,得到count的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Structure of Fenwick tree
struct FenwickTree {
// Binary indexed tree
vector bit;
// bit[0] is not involved
int n;
// Constructor
FenwickTree(int n)
{
this->n = n + 1;
bit = vector(n, 0);
}
// Constructor
FenwickTree(vector a)
: FenwickTree(a.size())
{
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
// Sum of arr[0] + arr[1] + ...
// + arr[idx] where arr is array
int sum(int idx)
{
int ret = 0;
// Index in BITree[] is 1 more
// than the index in arr[]
idx = idx + 1;
// Traverse the ancestors of
// BITree[index]
while (idx > 0) {
// Add current element
// of BITree to sum
ret += bit[idx];
// Move index to parent
// node in getSum View
idx -= idx & (-idx);
}
// Return the result
return ret;
}
// Function for adding arr[idx]
// is equals to arr[idx] + val
void add(int idx, int val)
{
// Val is nothing but "DELTA"
// index in BITree[] is 1
// more than the index in arr[]
idx = idx + 1;
// Traverse all ancestors
// and add 'val'
while (idx <= n) {
// Add 'val' to current
// node of BI Tree
bit[idx] += val;
// Update index to that
// of parent in update View
idx += idx & (-idx);
}
}
// Update the index
void update(int idx, int val)
{
add(idx, val - valat(idx));
}
// Perform the sum arr[l] + arr[l+1]
// + ... + arr[r]
int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
int valat(int i)
{
return sum(i, i);
}
void clr(int sz)
{
bit.clear();
n = sz + 1;
bit.resize(n + 1, 0);
// Initially mark all
// are present (i.e. 1)
for (int i = 0; i < n; i++)
add(i, 1);
}
};
// Function to count the characters
void charactersCount(string str, int n)
{
int i, count = 0;
// Store the values of indexes
// for each alphabet
vector > mp
= vector >(26,
vector());
// Create FenwickTree of size n
FenwickTree ft = FenwickTree(n);
ft.clr(n);
// Initially no indexes are
// stored for each character
for (i = 0; i < 26; i++)
mp[i].clear();
i = 0;
for (char ch : str) {
// Push 'i' to mp[ch]
mp[ch - 'a'].push_back(i);
i++;
}
// Start with smallest character
// and move towards larger character
// i.e., from 'a' to 'z' (0 to 25)
for (i = 0; i < 26; i++) {
// index(ind) of current character
// corres to i are obtained
// in increasing order
for (int ind : mp[i]) {
// Find the number of characters
// currently present before
// ind using ft.sum(ind)
count += ft.sum(ind);
// Mark the corresponding
// ind as removed and
// make value at ind as 0
ft.update(ind, 0);
}
}
// Print the value of count
cout << count << endl;
}
// Driver Code
int main()
{
// Given string str
string str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Structure of Fenwick tree
static class FenwickTree
{
// Binary indexed tree
int []bit;
// bit[0] is not involved
int n;
// Constructor
FenwickTree(int n)
{
this.n = n + 1;
bit = new int[n];
}
// Constructor
FenwickTree(Vector a)
{
for(int i = 0; i < a.size(); i++)
add(i, a.get(i));
}
// Sum of arr[0] + arr[1] + ...
// + arr[idx] where arr is array
int sum(int idx)
{
int ret = 0;
// Index in BITree[] is 1 more
// than the index in arr[]
idx = idx + 1;
// Traverse the ancestors of
// BITree[index]
while (idx > 0)
{
// Add current element
// of BITree to sum
ret += bit[idx];
// Move index to parent
// node in getSum View
idx -= idx & (-idx);
}
// Return the result
return ret;
}
// Function for adding arr[idx]
// is equals to arr[idx] + val
void add(int idx, int val)
{
// Val is nothing but "DELTA"
// index in BITree[] is 1
// more than the index in arr[]
idx = idx + 1;
// Traverse all ancestors
// and add 'val'
while (idx <= n)
{
// Add 'val' to current
// node of BI Tree
bit[idx] += val;
// Update index to that
// of parent in update View
idx += idx & (-idx);
}
}
// Update the index
void update(int idx, int val)
{
add(idx, val - valat(idx));
}
// Perform the sum arr[l] + arr[l+1]
// + ... + arr[r]
int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
int valat(int i)
{
return sum(i, i);
}
void clr(int sz)
{
n = sz + 1;
bit = new int[n + 1];
// Initially mark all
// are present (i.e. 1)
for(int i = 0; i < n; i++)
add(i, 1);
}
};
// Function to count the characters
static void charactersCount(String str, int n)
{
int i, count = 0;
// Store the values of indexes
// for each alphabet
@SuppressWarnings("unchecked")
Vector []mp = new Vector[26];
for(i = 0; i < mp.length; i++)
mp[i] = new Vector();
// Create FenwickTree of size n
FenwickTree ft = new FenwickTree(n);
ft.clr(n);
// Initially no indexes are
// stored for each character
for(i = 0; i < 26; i++)
mp[i].clear();
i = 0;
for(char ch : str.toCharArray())
{
// Push 'i' to mp[ch]
mp[ch - 'a'].add(i);
i++;
}
// Start with smallest character
// and move towards larger character
// i.e., from 'a' to 'z' (0 to 25)
for(i = 0; i < 26; i++)
{
// index(ind) of current character
// corres to i are obtained
// in increasing order
for(int ind : mp[i])
{
// Find the number of characters
// currently present before
// ind using ft.sum(ind)
count += ft.sum(ind);
// Mark the corresponding
// ind as removed and
// make value at ind as 0
ft.update(ind, 0);
}
}
// Print the value of count
System.out.print(count + "\n");
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Structure of Fenwick tree
public class FenwickTree
{
// Binary indexed tree
public int []bit;
// bit[0] is not involved
int n;
// Constructor
public FenwickTree(int n)
{
this.n = n + 1;
bit = new int[n];
}
// Constructor
public FenwickTree(List a)
{
for(int i = 0; i < a.Count; i++)
add(i, a[i]);
}
// Sum of arr[0] + arr[1] + ...
// + arr[idx] where arr is array
public int sum(int idx)
{
int ret = 0;
// Index in BITree[] is 1 more
// than the index in []arr
idx = idx + 1;
// Traverse the ancestors of
// BITree[index]
while (idx > 0)
{
// Add current element
// of BITree to sum
ret += bit[idx];
// Move index to parent
// node in getSum View
idx -= idx & (-idx);
}
// Return the result
return ret;
}
// Function for adding arr[idx]
// is equals to arr[idx] + val
public void add(int idx, int val)
{
// Val is nothing but "DELTA"
// index in BITree[] is 1
// more than the index in []arr
idx = idx + 1;
// Traverse all ancestors
// and add 'val'
while (idx <= n)
{
// Add 'val' to current
// node of BI Tree
bit[idx] += val;
// Update index to that
// of parent in update View
idx += idx & (-idx);
}
}
// Update the index
public void update(int idx, int val)
{
add(idx, val - valat(idx));
}
// Perform the sum arr[l] + arr[l+1]
// + ... + arr[r]
public int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
public int valat(int i)
{
return sum(i, i);
}
public void clr(int sz)
{
n = sz + 1;
bit = new int[n + 1];
// Initially mark all
// are present (i.e. 1)
for(int i = 0; i < n; i++)
add(i, 1);
}
};
// Function to count the characters
static void charactersCount(String str, int n)
{
int i, count = 0;
// Store the values of indexes
// for each alphabate
List []mp = new List[26];
for(i = 0; i < mp.Length; i++)
mp[i] = new List();
// Create FenwickTree of size n
FenwickTree ft = new FenwickTree(n);
ft.clr(n);
// Initially no indexes are
// stored for each character
for(i = 0; i < 26; i++)
mp[i].Clear();
i = 0;
foreach(char ch in str.ToCharArray())
{
// Push 'i' to mp[ch]
mp[ch - 'a'].Add(i);
i++;
}
// Start with smallest character
// and move towards larger chracter
// i.e., from 'a' to 'z' (0 to 25)
for(i = 0; i < 26; i++)
{
// index(ind) of current character
// corres to i are obtained
// in increasing order
foreach(int ind in mp[i])
{
// Find the number of characters
// currently present before
// ind using ft.sum(ind)
count += ft.sum(ind);
// Mark the corresponding
// ind as removed and
// make value at ind as 0
ft.update(ind, 0);
}
}
// Print the value of count
Console.Write(count + "\n");
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "aabbc";
int n = 5;
// Function call
charactersCount(str, n);
}
}
// This code is contributed by amal kumar choubey
5
时间复杂度: O(N*log(N))
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live