DEV Community

Cover image for Codewars: Isogram Problem
CLeeBenjamin
CLeeBenjamin

Posted on

4 3

Codewars: Isogram Problem

This problem on codewars ask the following:

An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.

Example: (Input --> Output)

isIsogram "Dermatoglyphics" = true
isIsogram "moose" = false
isIsogram "aba" = false
Enter fullscreen mode Exit fullscreen mode

How to approach it with PEDAC method:

P: Understand the problem -
Understand that isogram is a string that has no letter that repeats.

E: Give Example -
Creating your own example would be:
"apple" = false
"ted" = true
As I check each letter, I will need to place characters that were visited in a data structure.

D: What data structure(s) would be need -
What will I use to hold data if necessary:
Object

A: Steps to solve it without language specific information -

  • make string lowercase
  • check if string is empty and return true if so
  • loop through string
  • place item in data structure when visited
  • check if the character appears more than once in visited data structure, if it has return false
  • if looping finishes without returning, return true

C: The final code using the psuedocode (A) to guid me -

- make string lowercase

function isIsogram(str){
  let newStr = str.toLowerCase(); /// here
}
Enter fullscreen mode Exit fullscreen mode

- check if string is empty and return true if so

function isIsogram(str){
  let newStr = str.toLowerCase();

  if(newStr === ""){ /// here
    return true; /// here
  }  
}
Enter fullscreen mode Exit fullscreen mode

- loop through string

function isIsogram(str){
  let newStr = str.toLowerCase();

  if(newStr === ""){
    return true;
  }  

  for(let char of newStr){ /// here
    /* loop through the string */
  }
}
Enter fullscreen mode Exit fullscreen mode

- place item in data structure when visited

function isIsogram(str){
  let visited = {}; /// here
  let newStr = str.toLowerCase();

  if(newStr === ""){
    return true;
  }  

  for(let char of newStr){ 
    if(!visited[char]){ /// here
      visited[char] = 1 /// here
    }
}
Enter fullscreen mode Exit fullscreen mode

- check if the character appears more than once in visited data structure, if it has return false

function isIsogram(str){
  let visited = {};
  let newStr = str.toLowerCase();

  if(newStr === ""){
    return true;
  }  

  for(let char of newStr){
    if(!visited[char]){
      visited[char] = 1
    }else { /// here
      return false /// here
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

- if looping finishes without terminating the function, return true


function isIsogram(str){
  let visited = {};
  let newStr = str.toLowerCase();

  if(newStr === ""){
    return true;
  }  

  for(let char of newStr){
    if(!visited[char]){
      visited[char] = 1
    }else {
      return false
    }
  }
  return true; /// here
}
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay