String Flashcards

1
Q

Given a string, return true if it contains balanced parenthesis ().

“(x + y) - (4)” —- true

’)(‘ —- false

“(50)(“ —- false

A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Implement an algorithm to determine if a string has all unique characters. Do not use hash table?

A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Given two strings, write a method to decide if one is a permutation of the other?

A
  1. Determine if whitespace or case sensitivity is something to worry about?
  2. Verify perm definition through example, ‘dog’ is perm of ‘god’?
  3. If first string length !== second string, then return false.
  4. Convert string to array so we can use array methods. native split or for loop
  5. to use array methods
  6. sort the array. use native sort or for loop
  7. convert array to string. use native join or for loop
  8. 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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a method to replace all spaces in a string with ‘%20’?

A
  1. 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('')
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A
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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string ‘aabccccccaaa’ would become ‘a2b1c5a3’?

A
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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’)

A
function isSubstring(str1, str2){
 var s1 = str1 + str1;
 if(s1.indexOf(str2) \> 0){
 return true;
 }
 return false;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A
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(' ');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A
function end(str, target) {
 // "Never give up and good luck will find you."
 // -- Falcor
 return str.substr(-target.length) === target;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly