Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39300
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Writing Java code while high and drunk |
| Date | 2020-02-17 12:58 -0500 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <r2ek7g$qu3$1@gioia.aioe.org> (permalink) |
| References | <2542dd61-cde8-4a30-89be-2abfca122ac9@googlegroups.com> |
On 2/17/2020 9:57 AM, chad altenburg wrote:
(ignoring various non-Java pieces of information)> "
> Given a string, return true if the number of appearances of "is" anywhere in the string is equal to the number of appearances of "not" anywhere in the string (case sensitive).
>
> equalIsNot("This is not") → false
> equalIsNot("This is notnot") → true
> equalIsNot("noisxxnotyynotxisi") → true
> "
>
> Here is what I came up with...
>
> public boolean equalIsNot(String str) {
> int numberIs = 0;
> int numberNot = 0;
>
> for (int i = 0 ; i <= str.length() - 2; i++) {
> if (str.substring(i, i+2).equals("is") ) {
> numberIs++;
> }
> }
>
>
> for (int i = 0 ; i <= str.length() - 3; i++) {
> if (str.substring(i, i+3).equals("not") ) {
> numberNot++;
> }
> }
>
> if (numberIs == numberNot) return true;
> else return false;
> }
>
>
> And here is what I found via Google...
>
> https://github.com/mirandaio/codingbat/blob/master/java/string-3/equalIsNot.java
>
> I don't know which one is more correct.
They are pretty similar.
I believe there are at least 3 different approaches:
1) simple for loop and equals
2) a tricky loop and indexOf
3) regex
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Writing Java code while high and drunk chad altenburg <cdalten@gmail.com> - 2020-02-17 06:57 -0800 Re: Writing Java code while high and drunk Arne Vajhøj <arne@vajhoej.dk> - 2020-02-17 12:58 -0500 Re: Writing Java code while high and drunk Joerg Meier <joergmmeier@arcor.de> - 2020-02-17 22:48 +0100
csiph-web