String Flashcards
Given a string, return true if it contains balanced parenthesis ().
“(x + y) - (4)” —- true
’)(‘ —- false
“(50)(“ —- false
function isBalanced (string) { var counter = 0; if(string === ''"){ return true; } if(string[0] === ')'){ return false; } for(var i = 0; i \< string.length; i++){ if(string[i] === '('){ counter++; } else if(string[i] === ')'){ counter--; } else if(counter \< 0){ return false; } } return (counter === 0) ? true: false; }
Implement an algorithm to determine if a string has all unique characters. Do not use hash table?
function hasUniqueChars(str){ var arr = []; for (var i = 0; i \< str.length; i++) { if(arr[str[i]]){ return false; } arr[str[i]] = true; } return false; }
Given two strings, write a method to decide if one is a permutation of the other?
- Determine if whitespace or case sensitivity is something to worry about?
- Verify perm definition through example, ‘dog’ is perm of ‘god’?
- If first string length !== second string, then return false.
- Convert string to array so we can use array methods. native split or for loop
- to use array methods
- sort the array. use native sort or for loop
- convert array to string. use native join or for loop
- if str1 === str2, return true, else false
function isPermutation(str1, str2){
var arr1 = [];
var arr2 = [];
if(str1.length !== str2.length){
return false;
}
for (var i = 0; i < str1.length; i++) {
arr1.push(str1[i])
arr2.push(str2[i])
}
arr1.sort()
arr2.sort()
str1 = arr1.join(‘’)
str2 = arr2.join(‘’)
console.log(str1, str2)
return str1 === str2;
}
Write a method to replace all spaces in a string with ‘%20’?
- find out if a can use a separate result string or if they would like to be done with array
— using result string
function replaceSpaces(str) { var result = ''; for(var i = 0; i \< str.length; i++){ if(str[i] === ' '){ result+= '%20'; } else{ result += str[i]; } } return result }
function replaceSpaces(str) { var arr = str.split(''); for(var i = 0; i \< arr.length; i++){ if(arr[i] === ' '){ arr[i] = '%20'; } } return arr.join('') }
Given a string, write a function to check if it is a permutation of a palindrome? – string can have no more than one character that is odd. e.g. ‘taco cat’ -> {t: 2, a: 2, c: 2, a: 1} = only one char is odd. e.g. ‘test’ -> {t: 2, e: 1, s: 1} = more than one char is odd
function isPermOfPalindrome(str) { var ht = {}; var count = 0; for(var i = 0; i \< str.length; i++){ if(ht[str[i]] !== undefined){ ht[str[i]] = ht[str[i]] + 1; } else{ if(str[i] !== ' '){ ht[str[i]] = 1; }
}
}
for(var key in ht){
if(ht[key] % 2 === 1){
console.log(ht[key], key)
count++;
if(count > 1){
return false;
}
}
}
return true;
}
Given two strings, write a function to check if they are one edit(or zero edits) away?
pale, ple –> true
pales, pale –> true
pale, bale –> true
pale, bae –> false
function oneAway(str1, str2){ if(Math.abs(str1.length - str2.length) \> 1){ return false; } var index1 = 0; var index2 = 0; var foundDifference = false; while(index1 \< str1.length||index2 \< str2.length){ if(str1.charAt(index1) !== str2.charAt(index2)){ if(foundDifference){ return false; } foundDifference = true; } index1++ index2++ } return true; }
oneAway(‘yest’, ‘test’)
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string ‘aabccccccaaa’ would become ‘a2b1c5a3’?
function compress(str){ var result = ''; var count = 0; for(var i = 0; i \< str.length; i++){ count++; if(i + 1 \>= str.length || str.charAt(i) !== str.charAt(i + 1)){ result += str.charAt(i) result += count; count = 0; } } return result; }
Assume you have a method isSubString which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g. ‘waterbottle’ is a rotation of ‘erbottlewat’)
function isSubstring(str1, str2){ var s1 = str1 + str1; if(s1.indexOf(str2) \> 0){ return true; } return false; }
Title Case a Sentence
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
function titleCase(str) { var arr = str.split(' '); for(var i = 0; i <arr.length></arr.length> arr[i] = arr[i].toLowerCase(); arr[i] = arr[i][0].toUpperCase() + arr[i].substring(1); } return arr.join(' '); }
Check if a string (first argument) ends with the given target string (second argument).
end(“He has to give me a new name”, “name”) should return true.
function end(str, target) { // "Never give up and good luck will find you." // -- Falcor return str.substr(-target.length) === target; }