My solution to a “Daily Coding Problem” that I received in my mail today.

Given a string, determine whether any permutation of it is a palindrome.

For example, carrace should return true, since it can be rearranged to form racecar, which is a palindrome. daily should return false, since there’s no rearrangement that can form a palindrome.

Daily Coding Problem solution in Typescript,

permutationPalindrome(inputStr:string): boolean {
  let isPalindrome = true;
  if(inputStr == null || inputStr == undefined) {
    return false;
  }
  let occurenceMap = new Map<string, number>();
  for(let i = 0; i <  inputStr.length; i++) {
    let c = inputStr.charAt(i);
    if(occurenceMap.has(c)) {
      let count = occurenceMap.get(c) + 1;
      occurenceMap.set(c, count);
    } else {
      occurenceMap.set(c, 1);
    }
  }
  let occurenceCount = 0;
  occurenceMap.forEach((count, char) => {
    occurenceCount += count %2;
  })
  return occurenceCount <= 1;
}

Get updates?

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store. 

https://mydaytodo.com/apps/

Also, if you can leave a review on the App Store or Google Play Store, that would help too.

Categories: Algorithms

0 Comments

Leave a Reply

Verified by MonsterInsights