From 5f6510f0fa20bbf046fb0d9acd02673f10ecaa68 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 26 Aug 2024 08:55:50 +0200 Subject: [PATCH] refactor: `CharactersSame` (#5396) --- .../thealgorithms/strings/CharactersSame.java | 24 ++++++--------- .../strings/CharacterSameTest.java | 29 +++++-------------- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/CharactersSame.java b/src/main/java/com/thealgorithms/strings/CharactersSame.java index 78ccbbea..68785052 100644 --- a/src/main/java/com/thealgorithms/strings/CharactersSame.java +++ b/src/main/java/com/thealgorithms/strings/CharactersSame.java @@ -5,25 +5,19 @@ public final class CharactersSame { } /** - * Driver Code - */ - public static void main(String[] args) { - assert isAllCharactersSame(""); - assert !isAllCharactersSame("aab"); - assert isAllCharactersSame("aaa"); - assert isAllCharactersSame("11111"); - } - - /** - * check if all the characters of a string are same + * Checks if all characters in the string are the same. * * @param s the string to check - * @return {@code true} if all characters of a string are same, otherwise - * {@code false} + * @return {@code true} if all characters in the string are the same or if the string is empty, otherwise {@code false} */ public static boolean isAllCharactersSame(String s) { - for (int i = 1, length = s.length(); i < length; ++i) { - if (s.charAt(i) != s.charAt(0)) { + if (s.isEmpty()) { + return true; // Empty strings can be considered as having "all the same characters" + } + + char firstChar = s.charAt(0); + for (int i = 1; i < s.length(); i++) { + if (s.charAt(i) != firstChar) { return false; } } diff --git a/src/test/java/com/thealgorithms/strings/CharacterSameTest.java b/src/test/java/com/thealgorithms/strings/CharacterSameTest.java index d91b5f2f..98f822c4 100644 --- a/src/test/java/com/thealgorithms/strings/CharacterSameTest.java +++ b/src/test/java/com/thealgorithms/strings/CharacterSameTest.java @@ -1,28 +1,15 @@ package com.thealgorithms.strings; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; -public class CharacterSameTest { +class CharactersSameTest { - @Test - public void isAllCharactersSame() { - String input1 = "aaa"; - String input2 = "abc"; - String input3 = "1 1 1 1"; - String input4 = "111"; - String input5 = ""; - String input6 = " "; - String input7 = ". "; - - assertTrue(CharactersSame.isAllCharactersSame(input1)); - assertFalse(CharactersSame.isAllCharactersSame(input2)); - assertFalse(CharactersSame.isAllCharactersSame(input3)); - assertTrue(CharactersSame.isAllCharactersSame(input4)); - assertTrue(CharactersSame.isAllCharactersSame(input5)); - assertTrue(CharactersSame.isAllCharactersSame(input6)); - assertFalse(CharactersSame.isAllCharactersSame(input7)); + @ParameterizedTest + @CsvSource({"aaa, true", "abc, false", "'1 1 1 1', false", "111, true", "'', true", "' ', true", "'. ', false", "'a', true", "' ', true", "'ab', false", "'11111', true", "'ababab', false", "' ', true", "'+++', true"}) + void testIsAllCharactersSame(String input, boolean expected) { + assertEquals(CharactersSame.isAllCharactersSame(input), expected); } }