根据字符的 ASCII 值对字符串进行排序
给定一个大小为N的字符串S ,任务是根据字符串的 ASCII 值对字符串进行排序。
例子:
Input: S = “Geeks7”
Output: 7Geeks
Explanation: According to the ASCII values, integers comes first, then capital alphabets and the small alphabets.
Input: S = “GeeksForGeeks”
Output: FGGeeeekkorss
方法:解决这个问题的想法是维护一个数组来存储每个字符的频率,然后将它们相应地添加到结果字符串中。由于最多有256个字符,这使得空间复杂度保持不变。请按照以下步骤解决此问题:
- 用值0初始化大小为256的向量freq[]以存储字符串中每个字符的频率。
- 使用变量i遍历范围[0, N)并将数组freq[]中s[i]的计数增加1 。
- 将字符串S设为空字符串。
- 使用变量 i 迭代范围[0, N)并使用变量j迭代范围 [0, freq[ i ])并添加对应于字符s[]中第 i 个ASCII 值的字符串。
- 执行上述步骤后,打印字符串S作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to sort the string based
// on their ASCII values
void sortString(string s)
{
int N = s.length();
// Stores the frequency of each
// character of the string
vector freq(256, 0);
// Count and store the frequency
for (int i = 0; i < N; i++) {
freq[s[i]]++;
}
s = "";
// Store the result
for (int i = 0; i < 256; i++) {
for (int j = 0; j < freq[i]; j++)
s = s + (char)i;
}
// Print the result
cout << s << "\n";
return;
}
// Driver Code
int main()
{
string S = "GeeksForGeeks";
sortString(S);
return 0;
}
Java
// java program for the above approach
import java.util.*;
class Main
{
// Function to sort the string based
// on their ASCII values
static void sortString(String s)
{
int N = s.length();
// Stores the frequency of each
// character of the string
int [] freq = new int [256];
for(int i = 0; i < 256; i++){
freq[i] = 0;
}
// Count and store the frequency
for (int i = 0; i < N; i++) {
char character = s.charAt(i);
int val = (int) character;
freq[val]++;
}
// Store the result
for (int i = 0; i < 256; i++) {
for (int j = 0; j < freq[i]; j++)
// s = s + (char)i;
System.out.print((char)i);
}
}
// Driver Code
public static void main(String [] args)
{
String S = "GeeksForGeeks";
sortString(S);
}
}
// This code is contributed by amreshkumar3.
Python3
# Python Program to implement
# the above approach
# Function to sort the string based
# on their ASCII values
def sortString(s):
N = len(s)
# Stores the frequency of each
# character of the string
freq = [0] * 256
# Count and store the frequency
for i in range(0, N) :
freq[ord(s[i])] += 1
s = ""
# Store the result
for i in range(256):
for j in range(freq[i]):
s = s + chr(i)
# Print the result
print(s)
return
# Driver Code
S = "GeeksForGeeks"
sortString(S)
# This code is contributed by gfgking.
C#
// C# implementation for the above approach
using System;
class GFG
{
// Function to sort the string based
// on their ASCII values
static void sortString(string s)
{
int N = s.Length;
// Stores the frequency of each
// character of the string
int [] freq = new int[256];
for(int i = 0; i < 256; i++){
freq[i] = 0;
}
// Count and store the frequency
for (int i = 0; i < N; i++) {
char character = s[i];
int val = (int) character;
freq[val]++;
}
// Store the result
for (int i = 0; i < 256; i++) {
for (int j = 0; j < freq[i]; j++)
// s = s + (char)i;
Console.Write((char)i);
}
}
// Driver Code
public static void Main()
{
string S = "GeeksForGeeks";
sortString(S);
}
}
// This code is contributed by sanjoy_62.
Javascript
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Comparator Function to sort the given
// string in increasing order of their
// ASCII value
bool cmp(char ch, char chh) { return int(ch) <= int(chh); }
// Function to sort the string based
// on their ASCII values
string sortString(string S)
{
// Sort the string S
sort(S.begin(), S.end(), cmp);
return S;
}
// Driver Code
int main()
{
string S = "GeeksForGeeks";
cout << sortString(S);
return 0;
}
Python3
# Python Program to implement
# the above approach
# Function to sort the string based
# on their ASCII values
def sortString(s):
N = len(s)
# Stores the frequency of each
# character of the string
freq = [0] * 256
# Count and store the frequency
for i in range(0, N) :
freq[ord(s[i])] += 1
s = ""
# Store the result
for i in range(256):
for j in range(freq[i]):
s = s + chr(i)
# Print the result
print(s)
return
# Driver Code
S = "GeeksForGeeks"
sortString(S)
# This code is contributed by code_hunt.
输出:
FGGeeeekkorss
时间复杂度: O(256*N)
辅助空间: O(256)
替代方法:给定的问题也可以通过使用带有内置 sort()函数的比较器函数来解决,根据它们的 ASCII 值对给定的字符串进行排序。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Comparator Function to sort the given
// string in increasing order of their
// ASCII value
bool cmp(char ch, char chh) { return int(ch) <= int(chh); }
// Function to sort the string based
// on their ASCII values
string sortString(string S)
{
// Sort the string S
sort(S.begin(), S.end(), cmp);
return S;
}
// Driver Code
int main()
{
string S = "GeeksForGeeks";
cout << sortString(S);
return 0;
}
Python3
# Python Program to implement
# the above approach
# Function to sort the string based
# on their ASCII values
def sortString(s):
N = len(s)
# Stores the frequency of each
# character of the string
freq = [0] * 256
# Count and store the frequency
for i in range(0, N) :
freq[ord(s[i])] += 1
s = ""
# Store the result
for i in range(256):
for j in range(freq[i]):
s = s + chr(i)
# Print the result
print(s)
return
# Driver Code
S = "GeeksForGeeks"
sortString(S)
# This code is contributed by code_hunt.
输出:
FGGeeeekkorss
时间复杂度: O(N*log N)
辅助空间: O(1)