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
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 }
- check if string is empty and return true if so
function isIsogram(str){ let newStr = str.toLowerCase(); if(newStr === ""){ /// here return true; /// here } }
- loop through string
function isIsogram(str){ let newStr = str.toLowerCase(); if(newStr === ""){ return true; } for(let char of newStr){ /// here /* loop through the string */ } }
- 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 } }
- 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 } } }
- 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 }
Top comments (0)