-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6554 from chartypes/main
#9-Java
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Retos/Reto #9 - HETEROGRAMA, ISOGRAMA Y PANGRAMA [Fácil]/java/chartypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class Main { | ||
public static void main(String[] args) { | ||
System.out.println(Validator.heterogram("docu menta ril,y")); | ||
System.out.println(Validator.pangram("Blowzy nightfrumps vexd Jack Q")); | ||
System.out.println(Validator.isogram("Caucasus")); | ||
|
||
} | ||
} | ||
|
||
class Validator { | ||
|
||
public static boolean isogram(String text) { | ||
Map<Character, Integer> counter = new HashMap<>(); | ||
for (char letter : text.toLowerCase().toCharArray()) { | ||
|
||
if (!counter.containsKey(letter)) | ||
counter.put(letter, 1); | ||
else { | ||
counter.put(letter, counter.get(letter) + 1); | ||
} | ||
|
||
} | ||
counter.remove(' '); | ||
Object[] values = counter.values().toArray(); | ||
for (int i = 0; i < values.length; i++) { | ||
if (i == 0) | ||
continue; | ||
if (values[i] != values[i - 1]) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public static boolean pangram(String text) { | ||
List<Character> ALPHABET = List.of('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | ||
'Q', | ||
'R', | ||
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' '); | ||
for (char letter : text.toUpperCase().toCharArray()) | ||
if (!ALPHABET.contains(letter)) | ||
return false; | ||
return true; | ||
} | ||
|
||
public static boolean heterogram(String text) { | ||
char[] arrayText = text.trim().toCharArray(); | ||
for (int i = 0; i < arrayText.length; i++) { | ||
if (i == 0 || arrayText[i] == ' ') | ||
continue; | ||
int counter = 0; | ||
for (char j : arrayText) | ||
if (j == arrayText[i]) { | ||
counter++; | ||
if (counter > 1) | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |