[JAVA-5980] First Non Repeating Character (#13072)
* [JAVA-5980] First Non Repeating Character * [JAVA-5980] renamed UnitTest class * [JAVA-5980] changed file location to core-java-string-algorithms-3 Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
c5944c1d3d
commit
d74259b014
|
@ -0,0 +1,72 @@
|
||||||
|
package com.baeldung.firstnonrepeatingcharacter;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class FirstNonRepeatingCharacter {
|
||||||
|
public Character firstNonRepeatingCharBruteForce(String inputString) {
|
||||||
|
if (null == inputString || inputString.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (Character c : inputString.toCharArray()) {
|
||||||
|
int indexOfC = inputString.indexOf(c);
|
||||||
|
if (indexOfC == inputString.lastIndexOf(c)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Character firstNonRepeatingCharBruteForceNaive(String inputString) {
|
||||||
|
if (null == inputString || inputString.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (int outer = 0; outer < inputString.length(); outer++) {
|
||||||
|
boolean repeat = false;
|
||||||
|
for (int inner = 0; inner < inputString.length(); inner++) {
|
||||||
|
if (inner != outer && inputString.charAt(outer) == inputString.charAt(inner)) {
|
||||||
|
repeat = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!repeat) {
|
||||||
|
return inputString.charAt(outer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Character firstNonRepeatingCharWithMap(String inputString) {
|
||||||
|
if (null == inputString || inputString.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Map<Character, Integer> frequency = new HashMap<>();
|
||||||
|
for (int outer = 0; outer < inputString.length(); outer++) {
|
||||||
|
char character = inputString.charAt(outer);
|
||||||
|
frequency.put(character, frequency.getOrDefault(character, 0) + 1);
|
||||||
|
}
|
||||||
|
for (Character c : inputString.toCharArray()) {
|
||||||
|
if (frequency.get(c) == 1) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Character firstNonRepeatingCharWithArray(String inputString) {
|
||||||
|
if (null == inputString || inputString.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int[] frequency = new int[26];
|
||||||
|
for (int outer = 0; outer < inputString.length(); outer++) {
|
||||||
|
char character = inputString.charAt(outer);
|
||||||
|
frequency[character - 'a']++;
|
||||||
|
}
|
||||||
|
for (Character c : inputString.toCharArray()) {
|
||||||
|
if (frequency[c - 'a'] == 1) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.firstnonrepeatingcharacter;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class FirstNonRepeatingCharacterUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNonRepeatingCharacterBruteForce() {
|
||||||
|
FirstNonRepeatingCharacter program = new FirstNonRepeatingCharacter();
|
||||||
|
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharBruteForce("baeldung"), Character.valueOf('b'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharBruteForce("lullaby"), Character.valueOf('u'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharBruteForce("hello"), Character.valueOf('h'));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharBruteForce("mahimahi"));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharBruteForce(""));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharBruteForce(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNonRepeatingCharacterBruteForceNaive() {
|
||||||
|
FirstNonRepeatingCharacter program = new FirstNonRepeatingCharacter();
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharBruteForceNaive("baeldung"), Character.valueOf('b'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharBruteForceNaive("lullaby"), Character.valueOf('u'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharBruteForceNaive("hello"), Character.valueOf('h'));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharBruteForceNaive("mahimahi"));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharBruteForceNaive(""));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharBruteForceNaive(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNonRepeatingCharacterWithMap() {
|
||||||
|
FirstNonRepeatingCharacter program = new FirstNonRepeatingCharacter();
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharWithMap("baeldung"), Character.valueOf('b'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharWithMap("lullaby"), Character.valueOf('u'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharWithMap("hello"), Character.valueOf('h'));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharWithMap("mahimahi"));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharWithMap(""));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharWithMap(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNonRepeatingCharacterWithArray() {
|
||||||
|
FirstNonRepeatingCharacter program = new FirstNonRepeatingCharacter();
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharWithArray("baeldung"), Character.valueOf('b'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharWithArray("lullaby"), Character.valueOf('u'));
|
||||||
|
Assert.assertEquals(program.firstNonRepeatingCharWithArray("hello"), Character.valueOf('h'));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharWithArray("mahimahi"));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharWithArray(""));
|
||||||
|
Assert.assertNull(program.firstNonRepeatingCharWithArray(null));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue