Created
June 7, 2025 09:32
-
-
Save SuryaPratapK/c68b6bb99f0b860b9f30231ec9815c33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
string clearStars(string s) { | |
vector<vector<int>> char_pos(26); | |
for(int i=0;s[i];++i){ | |
if(s[i]=='*'){ | |
for(int j=0;j<26;++j){ | |
if(char_pos[j].size()>0){ | |
s[char_pos[j].back()]='*'; | |
char_pos[j].pop_back(); | |
break; | |
} | |
} | |
}else{ | |
char_pos[s[i]-'a'].push_back(i); | |
} | |
} | |
string res; | |
for(char c: s) | |
if(c!='*') | |
res.push_back(c); | |
return res; | |
} | |
}; | |
/* | |
//JAVA | |
import java.util.*; | |
class Solution { | |
public String clearStars(String s) { | |
List<List<Integer>> charPos = new ArrayList<>(); | |
for (int i = 0; i < 26; i++) { | |
charPos.add(new ArrayList<>()); | |
} | |
char[] arr = s.toCharArray(); | |
for (int i = 0; i < arr.length; i++) { | |
if (arr[i] == '*') { | |
for (int j = 0; j < 26; j++) { | |
if (!charPos.get(j).isEmpty()) { | |
int pos = charPos.get(j).remove(charPos.get(j).size() - 1); | |
arr[pos] = '*'; | |
break; | |
} | |
} | |
} else { | |
charPos.get(arr[i] - 'a').add(i); | |
} | |
} | |
StringBuilder res = new StringBuilder(); | |
for (char c : arr) { | |
if (c != '*') { | |
res.append(c); | |
} | |
} | |
return res.toString(); | |
} | |
} | |
#Python | |
class Solution: | |
def clearStars(self, s: str) -> str: | |
char_pos = [[] for _ in range(26)] | |
s_list = list(s) | |
for i, c in enumerate(s_list): | |
if c == '*': | |
for j in range(26): | |
if char_pos[j]: | |
pos = char_pos[j].pop() | |
s_list[pos] = '*' | |
break | |
else: | |
char_pos[ord(c) - ord('a')].append(i) | |
res = [] | |
for c in s_list: | |
if c != '*': | |
res.append(c) | |
return ''.join(res) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really Helpful!