给定一个字符串S,任务是把所有的数字出现在字符串中,该字符串的开头。
例子:
Input: S = “Geeks4forGeeks123”
Output: 4123GeeksforGeeks
Explanation:
The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the string to “4123GeeksforGeeks”.
Input: S = “GeeksforGeeks1234 A Com56puter Science Port7al”
Output: 1234567GeeksforGeeks A Computer Science Portal
做法:思路是遍历字符串,维护两个字符串,一个字符串包含数字,另一个字符串包含非数字字符。最后,按所需顺序将两个字符串附加到结果中。
下面是上述方法的实现:
Java
// Java program for the above approach
class GFG {
// Function to move all the digit
// to the beginning of the string
static void moveAllDigitAtBeginning(
String str)
{
// Calculate the string length
int len = str.length();
// Stores the digits
StringBuilder digits
= new StringBuilder();
// Stores the non-numeric character
StringBuilder nonNumericCharacter
= new StringBuilder();
// Traverse the string and
// check if there is a digit
for (char c : str.toCharArray()) {
// If character is a digit,
// add it to the string digits
if (c >= 48 && c <= 57) {
digits.append(c);
}
// Otherwise, add it to the
// string nonNumericCharacter
else {
nonNumericCharacter.append(c);
}
}
// Append both the strings
digits.append(
nonNumericCharacter.toString());
// Print the string
System.out.print(
digits.toString() + " ");
}
// Driver Code
public static void main(String args[])
{
// Given String str
String str
= "GeeksforGeeks123";
moveAllDigitAtBeginning(str);
}
}
Javascript
Java
// Java program for the above approach
class GFG {
// Function to move all the digit
// at the beginning of the string
static void moveAllDigitAtBeginning(
String str)
{
// Replace all the non-numeric
// characters with " " and
// replace all digits with " "
String moveAllDigit = str.replaceAll("\\D+", "")
+ str.replaceAll("\\d+", "");
// Print the string
System.out.println(moveAllDigit);
}
// Driver Code
public static void main(String args[])
{
// Given String str
String str
= "GeeksforGeeks1234";
moveAllDigitAtBeginning(str);
}
}
C#
// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG
{
// Function to move all the digit
// at the beginning of the string
static void moveAllDigitAtBeginning(
String str)
{
// Replace all the non-numeric
// characters with " " and
// replace all digits with " "
String moveAllDigit = Regex.Replace(str, "\\D+", "")
+Regex.Replace(str, "\\d", "");
// Print the string
Console.WriteLine(moveAllDigit);
}
// Driver Code
public static void Main(String []args)
{
// Given String str
String str
= "GeeksforGeeks1234";
moveAllDigitAtBeginning(str);
}
}
// This code is contributed by 29AjayKumar
输出:
123GeeksforGeeks
时间复杂度: O(N)
辅助空间: O(1)
基于正则表达式的方法:也可以使用正则表达式解决给定的问题,用空字符串(“”)替换所有非数字字符,给你所有数字,用空字符串(“”)替换所有数字,给你所有非数字字符。最后,连接字符串并打印结果。
下面是上述方法的实现:
Java
// Java program for the above approach
class GFG {
// Function to move all the digit
// at the beginning of the string
static void moveAllDigitAtBeginning(
String str)
{
// Replace all the non-numeric
// characters with " " and
// replace all digits with " "
String moveAllDigit = str.replaceAll("\\D+", "")
+ str.replaceAll("\\d+", "");
// Print the string
System.out.println(moveAllDigit);
}
// Driver Code
public static void main(String args[])
{
// Given String str
String str
= "GeeksforGeeks1234";
moveAllDigitAtBeginning(str);
}
}
C#
// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG
{
// Function to move all the digit
// at the beginning of the string
static void moveAllDigitAtBeginning(
String str)
{
// Replace all the non-numeric
// characters with " " and
// replace all digits with " "
String moveAllDigit = Regex.Replace(str, "\\D+", "")
+Regex.Replace(str, "\\d", "");
// Print the string
Console.WriteLine(moveAllDigit);
}
// Driver Code
public static void Main(String []args)
{
// Given String str
String str
= "GeeksforGeeks1234";
moveAllDigitAtBeginning(str);
}
}
// This code is contributed by 29AjayKumar
输出:
1234GeeksforGeeks
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live