Problem:
Return a version of the given string, where for every star (*) in the string the star and the chars immediately to its left and right are gone. So "ab*cd" yields "ad" and "ab**cd" also yields "ad".
starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"
starOut("ab*cd") → "ad"
starOut("ab**cd") → "ad"
starOut("sm*eilly") → "silly"
Solution:
public String starOut(String str) {
int len = str.length();
String finalString = "";
for (int i = 0; i < len; i++) {
if (i == 0 && str.charAt(i) != '*')
finalString += str.charAt(i);
if (i > 0 && str.charAt(i) != '*' && str.charAt(i-1) != '*')
finalString += str.charAt(i);
if (i > 0 && str.charAt(i) == '*' && str.charAt(i-1) != '*')
finalString = finalString.substring(0,finalString.length()-1);
}
return finalString;
}
My solution:
ReplyDeletepublic String starOut(String str) {
String out = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '*' || (i > 0 && str.charAt(i-1) == '*')) continue;
if (i < str.length()-1 && str.charAt(i+1) == '*') continue;
out = out + str.charAt(i);
}
return out;
}
i have been trying to use the String.replaceAll() method.
ReplyDeletehowever I cant figure out the regex to select all none 'word' chars.
try this one: str.replaceAll("(\\w?\\*\\w?)", "");
ReplyDeleteoh my goodness you are a legend
Deletewhere did you learn these type of solutions or are you creator of this site?
Delete@anonymous from where you have learn these trick plz share to us, it will be very helpfull.
DeleteThis is STAROUT solution into PLUSOUT topic.
ReplyDeletepublic String starOut(String str) {
ReplyDeleteStringBuilder sb = new StringBuilder("");
for(int i = 0; i < str.length(); i++) {
int left = (i >= 1) ? i - 1 : i;
int right = (i < str.length() - 1) ? i + 1 : i;
if(str.charAt(left) != '*' && str.charAt(right) != '*' && str.charAt(i) != '*') {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
return str.replaceAll("(.?[*]+.?)", "");
ReplyDeletehow do i know this shortcut
Deletehi all,
ReplyDeletei would like to use this code and it works correctly but not appending the last character of str. could you advise?
public String starOut(String str) {
String result = "";
int i=0;
while (i<str.length()-1)
{
if (str.substring(i,i+1).equals("*"))
{
result = result;
i=i+2;
}
else if (str.substring(i+1,i+2).equals("*"))
{
result = result;//.substring(0,i-1);
i=i+3;
}
else
{
result = result+str.substring(i,i+1);
i++;
}
}
return result;
}
This was my solution. It involves more lines of code, but has one fewer "if" statement within the for loop. So, perhaps it is more efficient?
ReplyDeletepublic String starOut(String str) {
String result = "";
//deal with empty strings;
if(str.length() < 1){
return result;
}
//advance starting index if str starts with '*'
int start = 0;
if(str.charAt(0) == '*'){
start++;
}
for(int i = start; i < str.length(); i++){
if(i > 0 && str.charAt(i-1) == '*'){
continue;
}
result += str.charAt(i);
if(str.charAt(i) == '*'){
result = result.substring(0, result.length()-2);
i++;
}
}
return result;
}
return str.replaceAll("[a-zA-Z1-9]?\\*+[a-zA-Z1-9]?", "");
ReplyDeleteCan you explain this regex expression
Deletepublic String starOut(String str) {
ReplyDeleteString rep = "";
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) != '*')
{
if (i == 0)
{
if (i + 1 < str.length() && str.charAt(i + 1) != '*')
{
rep += str.charAt(i);
}
else if (str.length() == 1)
{
return str;
}
else
{
i += 2;
}
}
else if (i == str.length() - 1)
{
if (i - 1 >= 0 && str.charAt(i - 1) != '*')
{
rep += str.charAt(i);
}
}
else
{
if (str.charAt(i - 1) != '*' && str.charAt(i + 1) != '*')
{
rep += str.charAt(i);
}
}
}
}
return rep;
}
public String starOut(String str) {
ReplyDeleteString star = "";
for (int i = 0; i < str.length(); i++)
{
if (!(str.charAt(i) == '*' || (i + 1 < str.length() && str.charAt(i + 1) == '*') || (i - 1 >= 0 && str.charAt(i - 1) == '*')))
{
star += str.charAt(i);
}
//For faster implementation Below//
else if (str.charAt(i) == '*')
{
i += 1;
}
else if (i + 1 < str.length() && str.charAt(i + 1) == '*')
{
i += 2;
}
}
return star;
}
public String starOut(String str) {
ReplyDeleteif(str.length() >= 3){
for(int i = 1; i < str.length()-1; i++){
if(str.charAt(i) == '*' && str.charAt(i+1) != '*'){
str = str.substring(0, i-1)+str.substring(i+2);
if(str.length() == 0){
return str;
}
i = i-1;
}
else if(str.charAt(i) == '*' && str.charAt(i+1) == '*'){
str = str.substring(0, i-1)+str.substring(i);
i = i-1;
}
}
if(str.charAt(0) == '*'){
str = str.substring(2);
}
if(str.charAt(str.length()-1) == '*'){
str = str.substring(0, str.length()-2);
}
}
if(str.length() == 1) {
if(str.charAt(0) == '*'){
str = "";
}
}
else if(str.length() == 2 && str.charAt(0) == '*' ||
str.length() == 2 && str.charAt(1) == '*'){
str = "";
}
return str;
}
return str.replaceAll("(.?[*]+.?)", "");
ReplyDeleteuhh blyaa
Delete