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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay