Problem:
Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
zipZap("zipXzap") → "zpXzp"
zipZap("zopzop") → "zpzp"
zipZap("zzzopzop") → "zzzpzp"
Solution:
public String zipZap(String str) { int len = str.length(); String finalString = ""; for (int i = 0; i < len; i++) { finalString += str.substring(i,i+1); if (i > 0 && i < len-1) { if (str.charAt(i-1) == 'z' && str.charAt(i+1) == 'p') finalString = finalString.substring(0,finalString.length()-1); } } return finalString; }
public String zipZap(String str) {
ReplyDeleteString first="";
String last="";
String temp="";
for(int i=0;i<str.length()-2;i++){
temp=str.substring(i,i+3);
if(temp.charAt(0)=='z' && temp.charAt(2)=='p'){
first=str.substring(0,i+1);
last=str.substring(i+2);
str=first+last;
}
}
return str;
}
public String zipZap(String str) {
DeleteString first="";
String last="";
String temp="";
String SOLVE = "";
for(int i=0;i<str.length()-2;i++){
temp=str.substring(i,i+3);
if(temp.charAt(0)=='z' && temp.charAt(2)=='p'){
first=str.substring(0,i+1);
last=str.substring(i+2);
SOLVE=SOLVE+first+last;
i = i+3;
}
SOLVE = SOLVE+ temp;
i = i+3;
}
return SOLVE;
}
Easy solution with StringBuilder
Deletepublic String zipZap(String str) {
StringBuilder sb = new StringBuilder(str);
for(int i = 0; i < sb.length() - 2; i++){
if(sb.charAt(i) == 'z' && sb.charAt(i+2) == 'p'){
sb.deleteCharAt(i+1);
}
}
String resultString = sb.toString();
return resultString;
}
Faster :P
Deletepublic String zipZap(String str) {
return str.replaceAll("z.p", "zp");
}
Definitely the quickest way --
ReplyDeletepublic String zipZap(String str) {
return str.replaceAll("z.p", "zp");
}
Thank you Jake I love you for this lol
DeleteAmazing solution! Thanks
DeleteAmazing jake
DeleteHow do I get this information and shortcuts
DeleteString result = "";
ReplyDeleteint i = 0;
while (i < str.length())
{
if (i != 0 && i != str.length() - 1 && str.charAt(i - 1) == 'z' && str.charAt(i + 1) == 'p')
{
i++;
}
else
{
result += str.charAt(i);
i++;
}
}
return result;
public String zipZap(String str) {
ReplyDeletefor(int i=0;i<str.length()-2;i++){
if(str.charAt(i)=='z'&&str.charAt(i+2)=='p'){str=str.substring(0,i+1)+str.substring(i+2);}
}
return str;
}
public String zipZap(String str) {
ReplyDeleteString temp = "";
int len = str.length();
if(str.length() <= 2)
{
return str;
}
for(int i=0;i<len-2;i++)
{
char s1 = str.charAt(i);
char s2 = str.charAt(i+2);
if(s1 == 'z' && s2 == 'p')
{
temp = temp + str.charAt(i) + str.charAt(i+2);
i = i+2;
}
else
{
temp = temp + str.charAt(i) ;
}
}
if(str.charAt(len -3) != 'z')
{
temp = temp + str.charAt(len-2) + str.charAt(len-1);
}
return temp ;
}
public String zipZap(String str) {
ReplyDeletefor (int i=0; i<str.length()-2; i++){
if(str.charAt(i) == 'z' && str.charAt(i+2) == 'p'){
str = str.substring(0, i+1) + str.substring(i+2);
}
}
return str;
}
//Just to add something for fun
ReplyDeletepublic String zipZap(String str) {
int[] indexArray = new int[str.length()];
String newStr="";
if(str.length()<3){
return str;
}
for (int i = 0; i<str.length(); i++){
indexArray[i]=0;
}
for (int i = 0; i<str.length()-2; i++){
if(str.charAt(i)=='z' && str.charAt(i+2)=='p'){
indexArray[i+1]=1;
}
}
for (int i = 0; i<str.length(); i++){
if(indexArray[i]==0){
newStr=newStr+str.charAt(i);
}
}
return newStr;
}
for (int i=0 ; i<str.length() -2 ; i++)
ReplyDelete{
if ( str.charAt(i)=='z' && str.charAt(i+2)=='p')
str= str.substring(0,i+1)+str.substring(i+2);
}
return str;
Recursion :
ReplyDeletepublic String zipZap(String str) {
if ( str.length() < 3 )
// Since the length of zip , zap or any z.p is 3
return str;
if( str.charAt(0)=='z' && str.charAt(2)=='p' )
return ""+str.charAt(0)+str.charAt(2)+zipZap(str.substring(3));
return str.charAt(0)+zipZap(str.substring(1));
}
public String zipZap(String str) {
ReplyDeleteString change = "";
for (int i = 0; i < str.length(); i++)
{
if (i + 2 < str.length() && str.charAt(i) == 'z' && str.charAt(i + 2) == 'p')
{
change += (str.substring(i, i+1) + str.substring(i + 2, i + 3));
i += 2;
}
else
{
change += str.charAt(i);
}
}
return change;
}
public String zipZap(String str) {
ReplyDeleteint l=str.length();
String res="";
if ( l<3 || (!str.contains("z") || !str.contains("p")) ) return str;
else {
for (int i=0; i<l; i++){
if (str.charAt(i)=='z' && str.charAt(i+2)=='p') {
res+="z";
res+="p";
i+=2;
}
else res= res+ str.charAt(i);
}
}
return res;
}
String s = "";
ReplyDeleteif(str.length() < 2) return str;
if(str.length() > 2)
{
for(int i = 1; i < str.length() - 1; i++)
{
if(str.substring(i-1,i).equals("z") && str.substring(i+1,i+2).equals("p"))
{
continue;
}
else s+=str.charAt(i);
}
}
return str.substring(0,1)+s+str.substring(str.length()-1);
public String zipZap(String str) {
ReplyDeleteString string = "";
for(int i = 0; i < str.length(); i++){
if ((i != 0 && i != str.length()-1) && //check i is not first or last char
(str.charAt(i - 1) == 'z' && str.charAt(i + 1) == 'p')){
} else {
string += str.charAt(i);
}
}
return string;
}
public String zipZap(String str) {
ReplyDeletereturn str.replaceAll("z[\\w]p", "zp");
}
public String zipZap(String str) {
ReplyDeleteString a="";
if(str.length()<=2) return str;
for(int i=0;i<str.length()-2;i++){
if(str.charAt(i)=='z' &&str.charAt(i+2)=='p'){
i=i+2; a=a+"zp";
}
else{a=a+str.charAt(i);}
}
if(str.charAt(str.length()-3)=='z' &&str.charAt(str.length()-1)=='p')
return a;
return a+ str.substring(str.length()-2);
}