diff --git a/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/firstnonrepeatingcharacter/FirstNonRepeatingCharacter.java b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/firstnonrepeatingcharacter/FirstNonRepeatingCharacter.java new file mode 100644 index 0000000000..e3ac0ea2f3 --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-3/src/main/java/com/baeldung/firstnonrepeatingcharacter/FirstNonRepeatingCharacter.java @@ -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 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; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/firstnonrepeatingcharacter/FirstNonRepeatingCharacterUnitTest.java b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/firstnonrepeatingcharacter/FirstNonRepeatingCharacterUnitTest.java new file mode 100644 index 0000000000..6fca176fa0 --- /dev/null +++ b/core-java-modules/core-java-string-algorithms-3/src/test/java/com/baeldung/firstnonrepeatingcharacter/FirstNonRepeatingCharacterUnitTest.java @@ -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)); + } +} \ No newline at end of file