2

I have a method that I want to return some path of a string example if I input : xxdrrryy - it should return rrr, I can only return a string of length 3, so I am trying this , but I'm stalked . it must be the occurrence of a letter three times consecutively

public String countTriple(String str) {
    int count = 1;
    char currChar = str.charAt(0);
    for(int i=1; i<str.length(); i++) {
        if(currChar == str.charAt(i)) {
            count++;
            if(count == 3) {
                StringBuilder sb = new StringBuilder("");
                for(int j=0; j<3;j++) {
                    sb.append(currChar);
                }
                return sb.toString();
            }
        }
        else {
            count = 1;
        }
        currChar = str.charAt(i);
    }
    return null; //no triple found
}
8
  • You should give the search in here a try Commented Dec 23, 2017 at 18:22
  • Try my answer. It's working. Originally there was a small bug, but its fine now. Commented Dec 23, 2017 at 18:28
  • yes I will now @Shn_Android_Dev Commented Dec 23, 2017 at 18:29
  • what if there are more then one match for example aaabcccdeee did you want just aaa or all aaa, ccc, eee ? Commented Dec 23, 2017 at 18:42
  • yes did not think of this situation , I was checking for one occurrence but that's a nice suggestion @YCF_L Commented Dec 23, 2017 at 19:32

3 Answers 3

2

Unless you have a specific reason not to, I suggest using a regex.

Something like this should suffice

Pattern p = Pattern.compile("(\\w)\\1\\1");
Matcher m = p.matcher("abbccc");
if(m.find()){
    System.out.println(m.group());
}

Just import java.util.regex.*

Sign up to request clarification or add additional context in comments.

2 Comments

yes that works thanks @Frank Underwood , can I get an up vote to remove a ban ?
@valik If it works, I suggest accepting and upvoting it
2

Please update your description it is very difficult to understand what you are trying to say. But as far as I am understanding you want to find out the count of a particular character in a string. Say you input "aabbbcccc" then it should return c has 4 characters or something like that.

If that is the case, then simple traverse over each character in that string and add them inside the HashTable and increase the count everytime the character is found, and return the value you require.

I hope this might help you.

4 Comments

if u input aabbbcccc it should return bbb because the rule is cccc has four occurrence and it should be maximum of three occurrence and not less @Ankit Kumar Singh
That's an easy task. You just need to add a check for that.
yes I am trying that right now @Ankit Kumar Singh can I get an up vote to remove a ban ?
for some reason it gives no return value @Ankit Kumar Singh
2

This code works. Try this:

public static String countTriple(String str) {

    int count = 1;
    char currChar = str.charAt(0);
    for(int i=1; i<str.length(); i++) {
        if(currChar == str.charAt(i)) {
            count++;
            if(count == 3) {
                StringBuilder sb = new StringBuilder("");
                for(int j=0; j<3;j++) {
                    sb.append(currChar);
                }
                return sb.toString();
            }
        }
        else {
            count = 1;
        }
        currChar = str.charAt(i);
    }
    return null; //no triple found
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.