Skip to content

Commit

Permalink
TypeScript can scramble
Browse files Browse the repository at this point in the history
  • Loading branch information
josephttran committed Oct 24, 2018
1 parent beba6db commit 42043bb
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions problems/can-scramble/can-scramble.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function sortString(str: string): string {
let strSorted: string = '';
let strArr: string[] = str.split('');

strSorted = strArr.sort().join('');

return strSorted;
}

function can_scramble(str1:string, str2:string): boolean {
if (str1.length !== str2.length) {
return false;
}

let str1Sorted = sortString(str1);
let str2Sorted = sortString(str2);

for (let i = 0; i < str1Sorted.length; i++) {
if (str1Sorted !== str2Sorted) {
return false;
}
}

return true;
}

console.log(can_scramble("abc", "cba")); // true
console.log(can_scramble("aab", "bba")); // false
console.log(can_scramble("xyzxyz", "zzyyxx")); //true
console.log(can_scramble("xyzbbxyz", "zzyyxxaa")); //false

0 comments on commit 42043bb

Please sign in to comment.