Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39300
| Path | csiph.com!aioe.org!.POSTED.O5hsyUvK01te0L/SZurVSQ.user.gioia.aioe.org!not-for-mail |
|---|---|
| From | Arne Vajhøj <arne@vajhoej.dk> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Writing Java code while high and drunk |
| Date | Mon, 17 Feb 2020 12:58:07 -0500 |
| Organization | Aioe.org NNTP Server |
| Lines | 49 |
| Message-ID | <r2ek7g$qu3$1@gioia.aioe.org> (permalink) |
| References | <2542dd61-cde8-4a30-89be-2abfca122ac9@googlegroups.com> |
| NNTP-Posting-Host | O5hsyUvK01te0L/SZurVSQ.user.gioia.aioe.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-Complaints-To | abuse@aioe.org |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 |
| Content-Language | en-US |
| X-Notice | Filtered by postfilter v. 0.9.2 |
| Xref | csiph.com comp.lang.java.programmer:39300 |
Show key headers only | View raw
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