Skip to main content
Tweeted twitter.com/StackCodeReview/status/659879587766272001
added 5 characters in body; edited tags; edited title
Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146

Longest Run String Programming Javarunning string

I came across this problem in a career cup. I came up with a solution or at least I think it works perfectly fine. Below I have the questions and the Java solution. I was just wondering if anyone could scrutinize my code and tell me if there are any improvements and, or better coding practices that I should know, as I am working on being able to produce proficient codes. Thank you all in advance.

Write a program to accept a nonempty string of alphanumeric characters. Define a “run” as a

consecutive sequence of a single character. For example, “aaaa” is a run of length 4. The program will

print the longest run in the given string. If there is no single longest run, then you may print any of

those runs whose length is at least as long as all other runs in the stringcode.

Example input: a

Example output: a

Example input: aab

Example output: aa

Example input: abbbbbcc

Example output: bbbbb

Example inputProblem statement: aabbccdd

Example output: aa

Write a program to accept a nonempty string of alphanumeric characters. Define a run as a consecutive sequence of a single character. For example, aaaa is a run of length \$4\$. The program will print the longest run in the given string. If there is no single longest run, then you may print any of those runs whose length is at least as long as all other runs in the string.

Example input: a 

Example output: a 

Example input: aab 

Example output: aa 

Example input: abbbbbcc 

Example output: bbbbb 

Example input: aabbccdd 

Example output: aa

Java Solution:

Longest Run String Programming Java

I came across this problem in career cup. I came up with a solution or at least I think it works perfectly fine. Below I have the questions and Java solution. I was just wondering if anyone could scrutinize my code and tell me if there are any improvements and better coding practices that I should know as I am working on being able to produce proficient codes. Thank you all in advance.

Write a program to accept a nonempty string of alphanumeric characters. Define a “run” as a

consecutive sequence of a single character. For example, “aaaa” is a run of length 4. The program will

print the longest run in the given string. If there is no single longest run, then you may print any of

those runs whose length is at least as long as all other runs in the string.

Example input: a

Example output: a

Example input: aab

Example output: aa

Example input: abbbbbcc

Example output: bbbbb

Example input: aabbccdd

Example output: aa

Java Solution:

Longest running string

I came across this problem in a career cup. Below I have the questions and the Java solution. I was wondering if anyone could scrutinize my code and tell me if there are any improvements, or better coding practices that I should know, as I am working on being able to produce proficient code.

Problem statement:

Write a program to accept a nonempty string of alphanumeric characters. Define a run as a consecutive sequence of a single character. For example, aaaa is a run of length \$4\$. The program will print the longest run in the given string. If there is no single longest run, then you may print any of those runs whose length is at least as long as all other runs in the string.

Example input: a 

Example output: a 

Example input: aab 

Example output: aa 

Example input: abbbbbcc 

Example output: bbbbb 

Example input: aabbccdd 

Example output: aa

Solution:

Post Migrated Here from stackoverflow.com (revisions)
Source Link
sophist_pt
  • 315
  • 2
  • 4

Longest Run String Programming Java

I came across this problem in career cup. I came up with a solution or at least I think it works perfectly fine. Below I have the questions and Java solution. I was just wondering if anyone could scrutinize my code and tell me if there are any improvements and better coding practices that I should know as I am working on being able to produce proficient codes. Thank you all in advance.

Write a program to accept a nonempty string of alphanumeric characters. Define a “run” as a

consecutive sequence of a single character. For example, “aaaa” is a run of length 4. The program will

print the longest run in the given string. If there is no single longest run, then you may print any of

those runs whose length is at least as long as all other runs in the string.

Example input: a

Example output: a

Example input: aab

Example output: aa

Example input: abbbbbcc

Example output: bbbbb

Example input: aabbccdd

Example output: aa

Java Solution:

public class StringRunCount {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(maxRun("aaabbbbbbbbcccccc"));
    }

    public static int maxRun(String testString) {
        int len = testString.length();
        String substr = "";
        for (int i = 0; i < len;) {
            char current = testString.charAt(i);
            String tempSubstr = "" + current;
            while ((i + 1) < len && (current == testString.charAt(i + 1))) {
                tempSubstr = tempSubstr + testString.charAt(++i);
            }

            if (tempSubstr.length() >= substr.length()) {
                substr = tempSubstr;

            }
            i += 1;
        }
        System.out.println(substr);
        return substr.length();
    }

}