Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.strings;

/**
* Longest Common Substring finds the longest string that is a
* contiguous substring of two input strings.
* Example: "abcdef" and "zcdemf" → "cde"
*/
public final class LongestCommonSubstring {

private LongestCommonSubstring() {
// Utility class
}

/**
* Finds the longest common substring of two strings.
*
* @param a First input string
* @param b Second input string
* @return The longest common substring, or empty string if none found
*/
public static String longestCommonSubstring(final String a, final String b) {
if (a == null || b == null || a.isEmpty() || b.isEmpty()) {
return "";
}
int[][] dp = new int[a.length() + 1][b.length() + 1];
int maxLength = 0;
int endIndex = 0;
for (int i = 1; i <= a.length(); i++) {
for (int j = 1; j <= b.length(); j++) {
if (a.charAt(i - 1) == b.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > maxLength) {
maxLength = dp[i][j];
endIndex = i;
}
}
}
}
return a.substring(endIndex - maxLength, endIndex);
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/thealgorithms/strings/TitleCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.thealgorithms.strings;

/**
* Title Case converts a string so that the first letter of each word
* is capitalized and the rest are lowercase.
* Example: "the quick brown fox" -> "The Quick Brown Fox"
*
* @see <a href="https://en.wikipedia.org/wiki/Title_case">
* Wikipedia: Title Case</a>
*/
public final class TitleCase {

private TitleCase() {
// Utility class
}

/**
* Converts a string to title case.
*
* @param input The string to convert
* @return The title-cased string, or empty string if input is null/empty.
* If input contains only whitespace, it is returned as is.
*/
public static String toTitleCase(final String input) {
if (input == null || input.isEmpty()) {
return "";
}
StringBuilder result = new StringBuilder();
boolean capitalizeNext = true;
for (char c : input.toCharArray()) {
if (Character.isWhitespace(c)) {
capitalizeNext = true;
result.append(c);
} else if (capitalizeNext) {
result.append(Character.toUpperCase(c));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(c));
}
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.thealgorithms.strings;

import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class LongestCommonSubstringTest {

private record TestData(String a, String b, String expected) {
}

private static Stream<TestData> provideTestCases() {
return Stream.of(
new TestData(null, "abc", ""),
new TestData("abc", null, ""),
new TestData("", "abc", ""),
new TestData("abc", "", ""),
new TestData("abcdef", "zcdemf", "cde"),
new TestData("abc", "abc", "abc"),
new TestData("abc", "xyz", ""),
new TestData("abcdef", "cdefgh", "cdef"),
new TestData("a", "a", "a")
);
}

@ParameterizedTest
@MethodSource("provideTestCases")
void testLongestCommonSubstring(TestData testData) {
Assertions.assertEquals(testData.expected(),
LongestCommonSubstring.longestCommonSubstring(testData.a(), testData.b()));
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/thealgorithms/strings/TitleCaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thealgorithms.strings;
// author: Vraj Prajapati @Rosander0

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class TitleCaseTest {

@Test
public void testNullOrEmptyInputs() {
assertEquals("", TitleCase.toTitleCase(null));
assertEquals("", TitleCase.toTitleCase(""));
}

@Test
public void testSingleWord() {
assertEquals("Hello", TitleCase.toTitleCase("hello"));
assertEquals("Hello", TitleCase.toTitleCase("HELLO"));
assertEquals("A", TitleCase.toTitleCase("a"));
}

@Test
public void testMultipleWords() {
assertEquals("The Quick Brown Fox", TitleCase.toTitleCase("the quick brown fox"));
assertEquals("The Quick Brown Fox", TitleCase.toTitleCase("THE QUICK BROWN FOX"));
assertEquals("Already Title Case", TitleCase.toTitleCase("already Title Case"));
}

@Test
public void testWhitespace() {
assertEquals(" Spaces ", TitleCase.toTitleCase(" spaces "));
}
}
Loading