📜  处理退格字符后的字符串

📅  最后修改于: 2022-05-13 01:57:01.017000             🧑  作者: Mango

处理退格字符后的字符串

给定一个包含字母和“ # ”的字符串S。 ' # ' 代表退格。任务是打印不带“ # ”的新字符串。
例子:

Input : S = "abc#de#f#ghi#jklmn#op#"
Output : abdghjklmo

Input : S = "##geeks##for##geeks#"
Output : geefgeek

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。

方法:通过使用 deque 来解决此问题的简单方法如下:



下面是上述方法的实现:

C++
// CPP implementation of above approach
#include 
using namespace std;
 
// Function to find new final String
string newString(string S)
{
    deque q;
 
    for (int i = 0; i < S.length(); ++i) {
 
        if (S[i] != '#')
            q.push_back(S[i]);
        else if (!q.empty())
            q.pop_back();
    }
 
    // build final string
    string ans = "";
 
    while (!q.empty()) {
        ans += q.front();
        q.pop_front();
    }
 
    // return final string
    return ans;
}
 
// Driver program
int main()
{
    string S = "##geeks##for##geeks#";
 
    // function call to print required answer
    cout << newString(S);
 
    return 0;
}
 
// This code is contributed by Sanjit_Prasad


Java
// Java implementation of above approach
import java.util.*;
class GFG
{
 
// Function to find new final String
static String newString(String S)
{
    Stack q = new Stack();
 
    for (int i = 0; i < S.length(); ++i)
    {
        if (S.charAt(i) != '#')
            q.push(S.charAt(i));
        else if (!q.isEmpty())
            q.pop();
    }
 
    // build final string
    String ans = "";
 
    while (!q.isEmpty())
    {
        ans += q.pop();
    }
 
    // return final string
    String answer = "";
    for(int j = ans.length() - 1; j >= 0; j--)
    {
        answer += ans.charAt(j);
    }
    return answer;
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "##geeks##for##geeks#";
 
    // function call to print
    // required answer
    System.out.println(newString(S));
}
}
 
// This code is contributed
// by prerna saini


Python3
# Python3 implementation of above approach
 
# Function to find new final String
def newString(S):
     
    q = []
 
    for i in range(0, len(S)):
 
        if S[i] != '#':
            q.append(S[i])
        elif len(q) != 0:
            q.pop()
 
    # Build final string
    ans = ""
 
    while len(q) != 0:
        ans += q[0]
        q.pop(0)
 
    # return final string
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    S = "##geeks##for##geeks#"
 
    # Function call to print
    # required answer
    print(newString(S))
     
# This code is contributed by Rituraj Jain


C#
// C# implementation of above approach
using System.Collections.Generic;
using System;
 
class GFG
{
 
// Function to find new final String
static String newString(String S)
{
    Stack q = new Stack();
 
    for (int i = 0; i < S.Length; ++i)
    {
        if (S[i] != '#')
            q.Push(S[i]);
        else if (q.Count!=0)
            q.Pop();
    }
 
    // build final string
    String ans = "";
 
    while (q.Count!=0)
    {
        ans += q.Pop();
    }
 
    // return final string
    String answer = "";
    for(int j = ans.Length - 1; j >= 0; j--)
    {
        answer += ans[j];
    }
    return answer;
}
 
// Driver Code
public static void Main(String []args)
{
    String S = "##geeks##for##geeks#";
 
    // function call to print
    // required answer
    Console.WriteLine(newString(S));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
geefgeek

时间复杂度: O(N),其中 N 是字符串的长度。