Skip to main content
replaced println with print
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    if (sublen > maxlen) {
        maxchar = prevchar;
        maxlen = sublen;
    }

    for (int i = maxlen; --i >= 0;)
        System.out.printlnprint(maxchar);
    System.out.println("");
    return maxlen;
}

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    if (sublen > maxlen) {
        maxchar = prevchar;
        maxlen = sublen;
    }

    for (int i = maxlen; --i >= 0;)
        System.out.println(maxchar);
    System.out.println("");
    return maxlen;
}

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    if (sublen > maxlen) {
        maxchar = prevchar;
        maxlen = sublen;
    }

    for (int i = maxlen; --i >= 0;)
        System.out.print(maxchar);
    System.out.println("");
    return maxlen;
}
forgot to handle the last run in a string
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    if (sublen > maxlen) {
        maxchar = prevchar;
        maxlen = sublen;
    }

    for (int i = maxlen; --i >= 0;)
        System.out.println(maxchar);
    System.out.println("");
    return maxlen;
}

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    for (int i = maxlen; --i >= 0;)
        System.out.println(maxchar);
    System.out.println("");
    return maxlen;
}

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    if (sublen > maxlen) {
        maxchar = prevchar;
        maxlen = sublen;
    }

    for (int i = maxlen; --i >= 0;)
        System.out.println(maxchar);
    System.out.println("");
    return maxlen;
}
code added
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    for (int i = maxlen; --i >= 0;)
        System.out.println(maxchar);
    System.out.println("");
    return maxlen;
}

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

Far too many Strings. Why don't you store just a single character and count its repetitions, then copy the char and the number to some maxchar and maxnum if number > maxnum?

EDIT (code added):

public static int maxRun(String testString) {
    int len = testString.length();
    char maxchar = "!";
    int  maxlen = 0;
    char prevchar = "!";
    int  sublen = 0;

    for (int i = 0; i < len; ++i) {
        char curr = testString.charAt(i);
        if (curr == prevchar) {
            ++sublen;
        }
        else
        {
            if (sublen > maxlen) {
                maxchar = prevchar;
                maxlen = sublen;
            }
            prevchar = curr;
            sublen = 1;
        }
    }

    for (int i = maxlen; --i >= 0;)
        System.out.println(maxchar);
    System.out.println("");
    return maxlen;
}
Source Link
CiaPan
  • 1.9k
  • 1
  • 12
  • 17
Loading