1
Sample Input #1

"tiptop"

Sample Output #1

tptp

Sample Input #2

"TipTop"

Sample Output #2

TipTop

Sample Input #3

"tpttipptptaptptopp"

Sample Output #3

tpttpptptptptpp

code

public String changeLetters(String str){
        String str1="";
        for(int i=0;i<str.length();i++){
            char ch1=str.charAt(i);
            char ch2=str.charAt(i+2);
            char ch3=str.charAt(i+1);
            if(str.length()==i+1)
            break;
            if((ch1==116)&&(ch2==112)){
                str1=str1+ch1+ch2;
                i=i+2;
            }else
              str1=str1+ch1;
        }
        return str1;

When I am trying to run my code only first test case "tiptop" runs while 2nd test case fails, can you suggest where I am doing mistake.

Testcase Pass/Fail Parameters Actual Output Expected Output

1 Pass 'tiptop' tptp tptp

2 Fail 'TipTop' null TipTop

Community
  • 1
  • 1
Pallavi Singh
  • 133
  • 10

3 Answers3

0

Use following code:

public static String changeLetters(String str) {
    String str1 = "";
    for (int i = 0; i < str.length(); i++) {
        if (i + 1 >= str.length()) {
            str1 += str.charAt(i);
            return str1;
        }
        char ch3 = '\u0000';
        char ch1 = str.charAt(i);
        char ch2 = str.charAt(i + 1);
        if (i + 2 < str.length()) {
            ch3 = str.charAt(i + 2);
        }
        if ((ch1 == 116) && (ch2 == 112)) {
            i++;
            str1 = str1 + ch1 + ch2;
        } else if ((ch1 == 116) && (ch2 != 112) && (ch3 == 112)) {
            i = i + 2;
            ch2 = ch3;
            str1 = str1 + ch1 + ch2;
        } else {
            str1 = str1 + ch1;
        }
    }
    return str1;
}

See the output: Ouput link

tpttpptptptptpp
TipTop

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
0

You need a simple regex :

    String[] s = {"tiptop", "TipTop", "tpttipptptaptptopp"} ;
    String regex = "(?<=t)[^pt](?=p)"; // any single character which is not 'p' or 't' and preceeded by t and followed by p
    for (String str : s) {
    System.out.println(str.replaceAll(regex, ""));
    }

O/P:

tptp
TipTop
tpttpptptptptpp
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0
public class LetterPattern {

 static String testcase1 = "tip.tip..tip";

 public static void main(String args[]){
 LetterPattern testInstance= new LetterPattern();
 String result = testInstance.changeLetters(testcase1);
 System.out.println(result);
 }

 public String changeLetters(String str)
 {
 //write your code here
    String str1 = "";
    int j = 0;
    int count = 2;
    for (int i=0; i<str.length(); i++)
    {

        j=i;
        // this will execute when 'tp'occurs that is when t and p are in continuty
         if (str.charAt(i) == 't' && str.charAt(j+1) == 'p') 
        {
            str1 = str1 + "tp";
            i= j+1;
        }

        // this will execute when t and p have one more letter in between, like tip, top. 
        else if (str.charAt(i) == 't' && str.charAt(j+2) == 'p') 
        {
            str1 = str1 + "tp";
            i= j+2;
        }

        // this will execute when there is no word like tp, top, tip which exactly mean all the words except tp, top, tip etc.
        else if (!((str.charAt(i) == 't' && str.charAt(j+1) == 'p'))
        {
            str1 = str1+str.charAt(i);
        }

    }
    return str1;
 } 
}