Skip to content

Instantly share code, notes, and snippets.

@ramonaharrison
Last active January 14, 2019 19:47
Show Gist options
  • Save ramonaharrison/a784ce1a796e3d0547b42d309ef447b2 to your computer and use it in GitHub Desktop.
Save ramonaharrison/a784ce1a796e3d0547b42d309ef447b2 to your computer and use it in GitHub Desktop.
package com.nytimes.android.buggyapp;
public class WordAnalyzer {
public WordAnalyzer() {
}
public char firstRepeatedCharacter(String word) {
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (ch == word.charAt(i + 1))
return ch;
}
return 0;
}
public char firstMultipleCharacter(String word) {
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (find(word, ch, i) >= 0)
return ch;
}
return 0;
}
private int find(String word, char c, int pos) {
for (int i = pos; i < word.length(); i++) {
if (word.charAt(i) == c) {
return i;
}
}
return -1;
}
public int countRepeatedCharacters(String word) {
int c = 0;
for (int i = 1; i < word.length() - 1; i++) {
if (word.charAt(i) == word.charAt(i + 1)) {
if (word.charAt(i - 1) != word.charAt(i)) {
c++;
}
}
}
return c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment