DEV Community

Discussion on: Daily Challenge #159 - Isogram

Collapse
 
aminnairi profile image
Amin • Edited

I might be wrong, but if you try with the word abcb which is not a word but just for the simplicity of the explanation, it would fail to find it using two pointers.

abcb
i  j continue

abcb
 ij   return true (but it's false here)

You have, in my understanding, no other choice but to check every letters for each letter pass, which make the worst case an O(n²) complexity. And I think the best case scenario for the complexity could be O(1) when the same letter is repeated twice in a row.

Here is what the algorithm could look like in JavaScript.

"use strict";

function isIsogram(word) {
    const length = word.length;

    for (let current = 0; current < length; current++) {
        for (let next = current + 1; next < length; next++) {
            if (word[current].toLowerCase() === word[next].toLowerCase()) {
                return false;
            }
        }
    }

    return true;
}

console.log(isIsogram("Dermatoglyphics")); // true
console.log(isIsogram("aba")); // false
console.log(isIsogram("moOse")); // false
console.log(isIsogram("abcb")); // false
Collapse
 
cuongnt_hn profile image
zack

So probably we can put all characters into Set then compare number of count. That is might be good solution.