Skip to main content

Command Palette

Search for a command to run...

Removing Stars From a String

Published
1 min read
Removing Stars From a String
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a string s, which contains stars *.

In one operation, you can:

  • Choose a star in s.

  • Remove the closest non-star character to its left, as well as remove the star itself.

Return the string after all stars have been removed.

Note:

  • The input will be generated such that the operation is always possible.

  • It can be shown that the resulting string will always be unique.

LeetCode Problem - 2390

class Solution {
    public String removeStars(String s) {
        StringBuilder sb = new StringBuilder(s); // Create a StringBuilder object to modify the string

        for (int i = 0; i < sb.length(); i++) {
            // If the current character is '*'
            if (sb.charAt(i) == '*') {
                // Remove the '*' character and the character before it
                sb.deleteCharAt(i);      // Remove the '*' character
                sb.deleteCharAt(i - 1);  // Remove the character before '*'
                i -= 2;                  // Move the index back by 2 to account for the removed characters
            }
        }

        // Convert the modified StringBuilder back to a string and return it
        return sb.toString();
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.

Removing Stars From a String