Path: csiph.com!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail From: nobody@example.org (Scott) Newsgroups: comp.lang.java.programmer Subject: Re: Writing Java code while drunk and stoned take 2 Date: Thu, 27 Feb 2020 16:39:40 GMT Organization: XMission http://xmission.com/ Message-ID: <5e57edbc.364432453@core> References: <1747103f-c38a-411e-92de-a44a52d9c761@googlegroups.com> Injection-Info: news.xmission.com; posting-host="shell.xmission.com:2607:fa18:0:beef::4"; logging-data="30748"; mail-complaints-to="abuse@xmission.com" X-Newsreader: Forte Free Agent 1.21/32.243 Xref: csiph.com comp.lang.java.programmer:39311 On Thu, 27 Feb 2020 06:00:53 -0800 (PST), chad altenburg wrote: >I was drunk and stoned because my girlfriend had just dump me. And I needed= > something to take my mind off the situation. So I attacked one problem tha= >t I couldn't solve on coding bat. Here is a link to the problem.. > >https://codingbat.com/prob/p180759 ... >The problem is that I have two loops. This is because I didn't know how to = >attack the case when I have to search for a pattern at the *end* of the str= >ing. If I use str.length(), I go out of bounds. So I ended up using a secon= >d loop to handle the end of the string case.=20 > >My gut feeling says this isn't right. But I have no idea how this could be = >done in one loop. You're close already. My solution omits the i++ clause from the for statement - what you do with i during each iteration depends on whether you found an instance of the pattern at str[i]. If you did, you advance i by three to consume the pattern; if not, advance by one to consume the non-matching character at that index. Append to newString appropriately in either case. After the loop, anything left in str is too short to match the pattern, so you just append it to newString. Two loops. My god, man, what would the neighbors say!?