从给定的字符串中删除空格
给定一个字符串,从字符串中删除所有空格并返回它。
Input: "g eeks for ge eeks "
Output: "geeksforgeeks"
预期的时间复杂度为 O(n) 并且仅遍历一次字符串。
我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。
下面是一个简单的解决方案
1) Iterate through all characters of given string, do following
a) If current character is a space, then move all subsequent
characters one position back and decrease length of the
result string.
上述解决方案的时间复杂度为 O(n 2 )。
更好的解决方案可以在 O(n) 时间内解决。这个想法是跟踪到目前为止看到的非空格字符的计数。
1) Initialize 'count' = 0 (Count of non-space character seen so far)
2) Iterate through all characters of given string, do following
a) If current character is non-space, then put this character
at index 'count' and increment 'count'
3) Finally, put '\0' at index 'count'
下面是上述算法的实现。
C++
// An efficient C++ program to remove all spaces
// from a string
#include
using namespace std;
// Function to remove all spaces from a given string
void removeSpaces(char *str)
{
// To keep track of non-space character count
int count = 0;
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
str[count] = '\0';
}
// Driver program to test above function
int main()
{
char str[] = "g eeks for ge eeks ";
removeSpaces(str);
cout << str;
return 0;
}
Java
// An efficient Java program to remove all spaces
// from a string
class GFG
{
// Function to remove all spaces
// from a given string
static int removeSpaces(char []str)
{
// To keep track of non-space character count
int count = 0;
// Traverse the given string.
// If current character
// is not space, then place
// it at index 'count++'
for (int i = 0; i
Python
# Python program to Remove spaces from a given string
# Function to remove all spaces from a given string
def removeSpaces(string):
# To keep track of non-space character count
count = 0
list = []
# Traverse the given string. If current character
# is not space, then place it at index 'count++'
for i in xrange(len(string)):
if string[i] != ' ':
list.append(string[i])
return toString(list)
# Utility Function
def toString(List):
return ''.join(List)
# Driver program
string = "g eeks for ge eeks "
print removeSpaces(string)
# This code is contributed by Bhavya Jain
C#
// An efficient C# program to remove all
// spaces from a string
using System;
class GFG
{
// Function to remove all spaces
// from a given string
static int removeSpaces(char []str)
{
// To keep track of non-space
// character count
int count = 0;
// Traverse the given string. If current
// character is not space, then place
// it at index 'count++'
for (int i = 0; i < str.Length; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
return count;
}
// Driver code
public static void Main(String[] args)
{
char []str = "g eeks for ge eeks ".ToCharArray();
int i = removeSpaces(str);
Console.WriteLine(String.Join("", str).Substring(0, i));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// CPP program to Remove spaces
// from a given string
#include
#include
using namespace std;
// Function to remove all spaces from a given string
string removeSpaces(string str)
{
str.erase(remove(str.begin(), str.end(), ' '), str.end());
return str;
}
// Driver program to test above function
int main()
{
string str = "g eeks for ge eeks ";
str = removeSpaces(str);
cout << str;
return 0;
}
// This code is contributed by Divyam Madaan
Java
// Java program to remove
// all spaces from a string
class GFG {
// Function to remove all
// spaces from a given string
static String removeSpace(String str)
{
str = str.replaceAll("\\s","");
return str;
}
// Driver Code
public static void main(String args[])
{
String str = "g eeks for ge eeks ";
System.out.println(removeSpace(str));
}
}
// This code is contributed by Kanhaiya.
Python
# Python program to Remove spaces from a given string
# Function to remove all spaces from a given string
def removeSpaces(string):
string = string.replace(' ','')
return string
# Driver program
string = "g eeks for ge eeks "
print(removeSpaces(string))
# This code is contributed by Divyam Madaan
C#
// C# program to remove
// all spaces from a string
using System;
class GFG
{
// Function to remove all
// spaces from a given string
static String removeSpace(String str)
{
str = str.Replace(" ","");
return str;
}
// Driver Code
public static void Main()
{
String str = "g eeks for ge eeks ";
Console.WriteLine(removeSpace(str));
}
}
// This code is contributed by
// PrinciRaj1992
Javascript
C++
#include
using namespace std;
int main()
{
string s = "g e e k s f o r g e e k s";
cout << "string with spaces is " << s << endl;
int l = s.length(); // storing the length of the string
int c
= count(s.begin(), s.end(),
' '); // counting the number of whitespaces
remove(s.begin(), s.end(),
' '); // removing all the whitespaces
s.resize(l - c); // resizing the string to l-c
cout << "string without spaces is " << s << endl;
return 0;
}
输出
geeksforgeeeks
上述解决方案的时间复杂度是 O(n) 并且它只对字符串进行一次遍历。
Divyam Madaan 建议的另一个解决方案是使用预定义函数。这是实现:
C++
// CPP program to Remove spaces
// from a given string
#include
#include
using namespace std;
// Function to remove all spaces from a given string
string removeSpaces(string str)
{
str.erase(remove(str.begin(), str.end(), ' '), str.end());
return str;
}
// Driver program to test above function
int main()
{
string str = "g eeks for ge eeks ";
str = removeSpaces(str);
cout << str;
return 0;
}
// This code is contributed by Divyam Madaan
Java
// Java program to remove
// all spaces from a string
class GFG {
// Function to remove all
// spaces from a given string
static String removeSpace(String str)
{
str = str.replaceAll("\\s","");
return str;
}
// Driver Code
public static void main(String args[])
{
String str = "g eeks for ge eeks ";
System.out.println(removeSpace(str));
}
}
// This code is contributed by Kanhaiya.
Python
# Python program to Remove spaces from a given string
# Function to remove all spaces from a given string
def removeSpaces(string):
string = string.replace(' ','')
return string
# Driver program
string = "g eeks for ge eeks "
print(removeSpaces(string))
# This code is contributed by Divyam Madaan
C#
// C# program to remove
// all spaces from a string
using System;
class GFG
{
// Function to remove all
// spaces from a given string
static String removeSpace(String str)
{
str = str.Replace(" ","");
return str;
}
// Driver Code
public static void Main()
{
String str = "g eeks for ge eeks ";
Console.WriteLine(removeSpace(str));
}
}
// This code is contributed by
// PrinciRaj1992
Javascript
输出
geeksforgeeeks
另一种使用预定义 STL 函数(如count() 、 remove() 、 getline()和resize() )解决此问题的方法也存在。这是相同的实现:
C++
#include
using namespace std;
int main()
{
string s = "g e e k s f o r g e e k s";
cout << "string with spaces is " << s << endl;
int l = s.length(); // storing the length of the string
int c
= count(s.begin(), s.end(),
' '); // counting the number of whitespaces
remove(s.begin(), s.end(),
' '); // removing all the whitespaces
s.resize(l - c); // resizing the string to l-c
cout << "string without spaces is " << s << endl;
return 0;
}
输出
string with spaces is g e e k s f o r g e e k s
string without spaces is geeksforgeeks