给定长度为N的字母数字字符串S ,任务是根据以下条件按优先级的升序对字符串进行排序:
- 甚至ASCII值的字符比奇数ASCII字符值更高的优先级。
- 偶数位的优先级高于奇数位。
- 数字比字符具有更高的优先级。
- 对于具有相同奇偶校验的字符或数字,优先级按其 ASCII 值的升序排列。
例子:
Input: S = “abcd1234”
Output: 1324bdac
Explanation:
The ASCII value of “a” is 97.
The ASCII value of “b” is 98.
The ASCII value of “c” is 99.
The ASCII value of “d” is 100.
Since characters with even ASCII value have higher priority, “b” and “d” are placed first followed by “a” and “c”.
Similarly, even digits have more priority than odd digits. Therefore, they are placed first.
Since the numbers have a higher priority than characters, the sorted string is “1324bdac”
Input: S = “adb123”
Output: bda132
方法:我们的想法是字符与奇数和偶数的ASCII值,也是数字与奇数和偶数校验分开。然后,按优先级顺序连接这些子串。请按照以下步骤解决问题:
- 初始化两个变量,比如数字和字符,分别存储字符和数字。
- 根据 ASCII 表对字符串数字和字符进行排序。
- 现在,遍历所有字符中的字符,并且每个字符与一个奇数奇偶校验到可变发言权oddChars并用偶校验另一个变量比如evenChars每个字符追加。
- 同样,对于字符串数字,将奇数和偶数奇偶校验数字分开,比如oddDigs和evenDigs 。
- 最后,将字符串连接为oddChars + evenChars +oddDigs + evenDigs 。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to sort the string s
// based on the given conditions
string sortString(string s)
{
// Stores digits of given string
string digits = "";
// Stores characters of given string
string character = "";
// Iterate over characters of the string
for (int i = 0; i < s.size(); ++i) {
if (s[i] >= '0' && s[i] <= '9')
{
digits += s[i];
}
else
{
character += s[i];
}
}
// Sort the string of digits
sort(digits.begin(), digits.end());
// Sort the string of characters
sort(character.begin(), character.end());
// Stores odd and even characters
string OddChar = "", EvenChar = "";
// Seperate odd and even digits
for (int i = 0; i < digits.length(); ++i) {
if ((digits[i] - '0') % 2 == 1) {
OddChar += digits[i];
}
else {
EvenChar += digits[i];
}
}
// Concatenate strings in the order
// odd characters followed by even
OddChar += EvenChar;
EvenChar = "";
// Seperate the odd and even chars
for (int i = 0; i < character.length();
++i) {
if ((character[i] - 'a') % 2 == 1) {
OddChar += character[i];
}
else {
EvenChar += character[i];
}
}
// Final string
OddChar += EvenChar;
// Return the final string
return OddChar;
}
// Driver Code
int main()
{
// Given string
string s = "abcd1234";
// Returns the sorted string
cout << sortString(s);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to sort the String s
// based on the given conditions
static String sortString(String s)
{
// Stores digits of given String
String digits = "";
// Stores characters of given String
String character = "";
// Iterate over characters of the String
for (int i = 0; i < s.length(); ++i) {
if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
digits += s.charAt(i);
} else {
character += s.charAt(i);
}
}
// Sort the String of digits
digits = sort(digits);
// Sort the String of characters
character = sort(character);
// Stores odd and even characters
String OddChar = "", EvenChar = "";
// Seperate odd and even digits
for (int i = 0; i < digits.length(); ++i) {
if ((digits.charAt(i) - '0') % 2 == 1) {
OddChar += digits.charAt(i);
} else {
EvenChar += digits.charAt(i);
}
}
// Concatenate Strings in the order
// odd characters followed by even
OddChar += EvenChar;
EvenChar = "";
// Seperate the odd and even chars
for (int i = 0; i < character.length(); ++i) {
if ((character.charAt(i) - 'a') % 2 == 1) {
OddChar += character.charAt(i);
} else {
EvenChar += character.charAt(i);
}
}
// Final String
OddChar += EvenChar;
// Return the final String
return OddChar;
}
static String sort(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
// return new sorted string
return new String(tempArray);
}
// Driver Code
public static void main(String[] args)
{
// Given String
String s = "abcd1234";
// Returns the sorted String
System.out.print(sortString(s));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to sort the string s
# based on the given conditions
def sortString(s):
# Stores digits of given string
digits = ""
# Stores characters of given string
character = ""
# Iterate over characters of the string
for i in range(len(s)):
if (s[i] >= '0' and s[i] <= '9'):
digits += s[i]
else:
character += s[i]
# Sort the string of digits
Digits = list(digits)
Digits.sort()
# Sort the string of characters
Character = list(character)
Character.sort()
# Stores odd and even characters
OddChar, EvenChar = "", ""
# Seperate odd and even digits
for i in range(len(Digits)):
if ((ord(digits[i]) - ord('0')) % 2 == 1):
OddChar += Digits[i]
else:
EvenChar += Digits[i]
# Concatenate strings in the order
# odd characters followed by even
OddChar += EvenChar
EvenChar = ""
# Seperate the odd and even chars
for i in range(len(Character)):
if ((ord(Character[i]) - ord('a')) % 2 == 1):
OddChar += Character[i]
else:
EvenChar += Character[i]
# Final string
OddChar += EvenChar
# Return the final string
return OddChar
# Driver Code
# Given string
s = "abcd1234"
# Returns the sorted string
print(sortString(list(s)))
# This code is contributed by divyesh072019
C#
// C# program for the above approach
using System;
class GFG
{
// Function to sort the string s
// based on the given conditions
static string sortString(char[] s)
{
// Stores digits of given string
string digits = "";
// Stores characters of given string
string character = "";
// Iterate over characters of the string
for (int i = 0; i < s.Length; ++i)
{
if (s[i] >= '0' && s[i] <= '9')
{
digits += s[i];
}
else
{
character += s[i];
}
}
// Sort the string of digits
char[] Digits = digits.ToCharArray();
Array.Sort(Digits);
// Sort the string of characters
char[] Character = character.ToCharArray();
Array.Sort(Character);
// Stores odd and even characters
string OddChar = "", EvenChar = "";
// Seperate odd and even digits
for (int i = 0; i < Digits.Length; ++i)
{
if ((digits[i] - '0') % 2 == 1)
{
OddChar += Digits[i];
}
else
{
EvenChar += Digits[i];
}
}
// Concatenate strings in the order
// odd characters followed by even
OddChar += EvenChar;
EvenChar = "";
// Seperate the odd and even chars
for (int i = 0; i < Character.Length; ++i)
{
if ((Character[i] - 'a') % 2 == 1)
{
OddChar += Character[i];
}
else
{
EvenChar += Character[i];
}
}
// Final string
OddChar += EvenChar;
// Return the final string
return OddChar;
}
// Driver code
static void Main()
{
// Given string
string s = "abcd1234";
// Returns the sorted string
Console.WriteLine(sortString(s.ToCharArray()));
}
}
// This code is contributed by divyehrabadiya07
Javascript
1324bdac
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live