refactor: WordLadder (#5434)

* refactor: WordLadder

* refactor: fix redundant check

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-30 09:49:55 +02:00
committed by GitHub
parent 87cf89192b
commit 14916e692f
2 changed files with 34 additions and 54 deletions

View File

@ -5,6 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class WordLadderTest {
@ -38,4 +40,12 @@ public class WordLadderTest {
List<String> wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);
}
@ParameterizedTest
@CsvSource({"'a', 'c', 'b,c', 2", "'a', 'c', 'a', 0", "'a', 'a', 'a', 0", "'ab', 'cd', 'ad,bd,cd', 3", "'a', 'd', 'b,c,d', 2", "'a', 'd', 'b,c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,d', 2"})
void testLadderLength(String beginWord, String endWord, String wordListStr, int expectedLength) {
List<String> wordList = List.of(wordListStr.split(","));
int result = WordLadder.ladderLength(beginWord, endWord, wordList);
assertEquals(expectedLength, result);
}
}