Exercism - Isogram
This post shows you how to get Isogram exercise of Exercism.
Preparation
Before we click on our next exercise, let’s see what concepts of DART we need to consider

So we need to use the following concepts.
String toLowerCase Method
The toLowerCase() method converts all characters in a string to lowercase. This is useful for case-insensitive comparisons.
void main() {
String word = "IsOgrAm";
// Convert to lowercase
String lower = word.toLowerCase();
print(lower); // "isogram"
// Use for case-insensitive processing
String processed = word.toLowerCase();
// Now all characters are lowercase for comparison
}
String replaceAll Method
The replaceAll() method replaces all occurrences of a pattern in a string with another string. It’s useful for removing or replacing characters.
void main() {
String word = "six-year-old";
// Remove hyphens
String noHyphens = word.replaceAll('-', '');
print(noHyphens); // "sixyearold"
// Remove spaces
String phrase = "six year old";
String noSpaces = phrase.replaceAll(' ', '');
print(noSpaces); // "sixyearold"
// Chain multiple replaceAll calls
String text = "six-year old";
String cleaned = text.replaceAll('-', '').replaceAll(' ', '');
print(cleaned); // "sixyearold"
}
String Split Method
The split() method divides a string into a list of substrings. When called with an empty string '', it splits the string into individual characters.
void main() {
String word = "isogram";
// Split into individual characters
List<String> chars = word.split('');
print(chars); // [i, s, o, g, r, a, m]
// Split after cleaning
String cleaned = "six-year-old".replaceAll('-', '');
List<String> chars2 = cleaned.split('');
print(chars2); // [s, i, x, y, e, a, r, o, l, d]
}
Sets
Sets are collections of unique elements. Converting a list to a set automatically removes duplicates. The toSet() method converts a list to a set.
void main() {
List<String> chars = ['i', 's', 'o', 'g', 'r', 'a', 'm'];
// Convert to set (removes duplicates if any)
Set<String> unique = chars.toSet();
print(unique); // {i, s, o, g, r, a, m}
// Example with duplicates
List<String> withDupes = ['i', 's', 'o', 'g', 'r', 'a', 'm', 's'];
Set<String> unique2 = withDupes.toSet();
print(unique2); // {i, s, o, g, r, a, m} (duplicate 's' removed)
print(withDupes.length); // 8
print(unique2.length); // 7 (one duplicate removed)
}
List Length Property
The length property returns the number of elements in a list or set. Comparing lengths can detect duplicates.
void main() {
List<String> chars = ['i', 's', 'o', 'g', 'r', 'a', 'm'];
Set<String> unique = chars.toSet();
// Compare lengths
print(chars.length); // 7
print(unique.length); // 7
print(chars.length == unique.length); // true (no duplicates)
// With duplicates
List<String> withDupes = ['i', 's', 'o', 'g', 'r', 'a', 'm', 's'];
Set<String> unique2 = withDupes.toSet();
print(withDupes.length); // 8
print(unique2.length); // 7
print(withDupes.length == unique2.length); // false (has duplicates)
}
Method Chaining
Method chaining allows you to call multiple methods in sequence. Each method operates on the result of the previous one, creating a pipeline of transformations.
void main() {
String word = "IsOgrAm";
// Chain multiple operations
String cleaned = word
.toLowerCase() // "isogram"
.replaceAll('-', '') // Remove hyphens (none here)
.replaceAll(' ', ''); // Remove spaces (none here)
// Split and convert to set
Set<String> unique = cleaned
.split('') // [i, s, o, g, r, a, m]
.toSet(); // {i, s, o, g, r, a, m}
// Check for duplicates
bool isIsogram = cleaned.split('').toSet().length == cleaned.split('').length;
print(isIsogram); // true
}
Comparison Operators
Comparison operators (==, !=) compare values and return boolean results. They’re used to check if two values are equal.
void main() {
int length1 = 7;
int length2 = 7;
int length3 = 8;
// Equality check
print(length1 == length2); // true
print(length1 == length3); // false
// Use to check for duplicates
List<String> chars = ['i', 's', 'o', 'g', 'r', 'a', 'm'];
Set<String> unique = chars.toSet();
bool noDupes = chars.length == unique.length;
print(noDupes); // true
}
Introduction
Determine if a word or phrase is an isogram.
An isogram (also known as a “non-pattern word”) is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.
Examples of Isograms
- lumberjacks - All letters are unique
- background - All letters are unique
- downstream - All letters are unique
- six-year-old - All letters are unique (hyphens are allowed to repeat)
Non-Isogram Example
The word isograms, however, is not an isogram, because the s repeats.
What is an isogram?
An isogram is a word or phrase in which no letter of the alphabet appears more than once. Spaces and hyphens are typically allowed to appear multiple times. Isograms are also known as “non-pattern words” and are used in various word games and puzzles. Famous examples include “lumberjacks”, “background”, and “downstream”.
— Word Games
How can we check if a word is an isogram?
To check if a word is an isogram:
- Normalize the input: Convert to lowercase for case-insensitive comparison
- Remove allowed characters: Remove hyphens and spaces (these can repeat)
- Split into characters: Convert the cleaned string to a list of characters
- Remove duplicates: Convert the list to a set (which automatically removes duplicates)
- Compare lengths: If the original list length equals the set length, there are no duplicate letters → it’s an isogram
The key insight is that if a list has duplicates, converting it to a set will reduce its length. If the lengths are equal, there are no duplicates.
For example, with “isogram”:
- Clean: “isogram” (no hyphens or spaces to remove)
- Split: [‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’]
- To set: {‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’}
- Compare: 7 == 7 → Isogram!
With “isograms”:
- Clean: “isograms”
- Split: [‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘s’]
- To set: {‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’} (duplicate ‘s’ removed)
- Compare: 8 != 7 → Not an isogram!
Solution
class Isogram {
bool isIsogram(String word) {
word = word.toLowerCase().replaceAll('-', '').replaceAll(' ', '');
return word.split('').toSet().length == word.split('').length;
}
}
Let’s break down the solution:
-
bool isIsogram(String word)- Main method that checks if a word is an isogram:- Takes a string (word or phrase) as input
- Returns
trueif it’s an isogram,falseotherwise
-
word = word.toLowerCase().replaceAll('-', '').replaceAll(' ', '')- Normalize and clean the input:.toLowerCase(): Converts all characters to lowercase for case-insensitive comparison- Example: “IsOgrAm” → “isogram”
.replaceAll('-', ''): Removes all hyphens (hyphens are allowed to repeat)- Example: “six-year-old” → “sixyearold”
.replaceAll(' ', ''): Removes all spaces (spaces are allowed to repeat)- Example: “six year old” → “sixyearold”
- Method chaining: All operations are chained together, each operating on the result of the previous one
- The cleaned word is stored back in the
wordvariable
-
return word.split('').toSet().length == word.split('').length- Check for duplicates:word.split(''): Splits the cleaned string into a list of individual characters- Example: “isogram” → [‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’]
.toSet(): Converts the list to a set, which automatically removes duplicates- Example: [‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’] → {‘i’, ‘s’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’}
- If there were duplicates, the set would have fewer elements
.length: Gets the number of elements in the setword.split('').length: Gets the number of elements in the original list==: Compares the two lengths- If equal: no duplicates were removed → isogram
- If different: duplicates were removed → not an isogram
The solution efficiently checks for duplicate letters by comparing the length of the character list with the length of its set representation. If they’re equal, all letters are unique and the word is an isogram.
You can watch this tutorial on YouTube. So don’t forget to like and subscribe. 😉
Watch on YouTube