Added function in Pangram.java (#3703)

* Added function in Pangram.java

Added isPangramIndexOf() function in Pangram.java

* Added Tests for new function

Added Tests for isPangramIndexOf() function

* fixed typo

* changed function name

* changed function name

Co-authored-by: Debasish Biswas <debasishbsws.abc@gmail.com>
This commit is contained in:
Taranjeet Singh Kalsi 2022-10-27 23:26:34 +05:30 committed by GitHub
parent b2393d62a0
commit acf7a86b96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 6 deletions

View File

@ -36,4 +36,23 @@ public class Pangram {
}
return true;
}
/**
* Checks if a String is Pangram or not by checking if each alhpabet is present or not
*
* @param s The String to check
* @return {@code true} if s is a Pangram, otherwise {@code false}
*/
public static boolean isPangram2(String s) {
if (s.length() < 26) {
return false;
}
s = s.toLowerCase(); // Converting s to Lower-Case
for (char i = 'a'; i <= 'z'; i++) {
if (s.indexOf(i) == -1) {
return false; // if any alphabet is not present, return false
}
}
return true;
}
}

View File

@ -1,17 +1,20 @@
package com.thealgorithms.strings;
import static com.thealgorithms.strings.Pangram.isPangram;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PangramTest {
@Test
public void testPangram() {
assertTrue(isPangram("The quick brown fox jumps over the lazy dog"));
assertFalse(isPangram("The quick brown fox jumps over the azy dog")); // L is missing
assertFalse(isPangram("+-1234 This string is not alphabetical"));
assertFalse(isPangram("\u0000/\\ Invalid characters are alright too"));
assertTrue(Pangram.isPangram("The quick brown fox jumps over the lazy dog"));
assertFalse(Pangram.isPangram("The quick brown fox jumps over the azy dog")); // L is missing
assertFalse(Pangram.isPangram("+-1234 This string is not alphabetical"));
assertFalse(Pangram.isPangram("\u0000/\\ Invalid characters are alright too"));
assertTrue(Pangram.isPangram2("The quick brown fox jumps over the lazy dog"));
assertFalse(Pangram.isPangram2("The quick brown fox jumps over the azy dog")); // L is missing
assertFalse(Pangram.isPangram2("+-1234 This string is not alphabetical"));
assertFalse(Pangram.isPangram2("\u0000/\\ Invalid characters are alright too"));
}
}