给定大小为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 alphabate
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 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
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 alphabate
@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 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
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)
高效方法:可以使用二叉索引树或Fenwick树的概念来解决此问题。步骤如下:
- 最初,以升序存储所有字符/ 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 alphabate
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 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
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 alphabate
@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 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
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)