Last active
January 14, 2019 19:47
-
-
Save ramonaharrison/a784ce1a796e3d0547b42d309ef447b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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