Question

In: Computer Science

C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...

C#

Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words.

Input: Tact Coa

Output: True (permutations: "taco cat", "atco cta", etc.)

Comment your code to explain it.

Solutions

Expert Solution

// if you have any problem let em know i will try to help you. Thank you.

using System;

class MainClass {

  public static bool isPallindromePermutation(string str)
  {
    str=str.ToLower();  // convert the string to lowercase
    int[] arr;
    arr=new int[26]; // an array to store the count of each character
    int odd=0; // count number of odd numbers times any character occurs
   for(int i=0;i<26;i++){
      arr[i]=0;
   }
    for(int i=0;i<str.Length;i++)
    {
      if(str[i]!=' '){   // it is not space
      int k=(str[i]-'a');
      arr[k]=arr[k]+1;
      }
    }
    foreach(int a in arr)
    {
      if(a%2!=0)
         odd++;
    }
    if(odd<=1)  // if there is only one character or no character with odd number of times occur then it is possible 
       return true;
    else
    return false;

  }

  public static void Main (string[] args) {

    Console.WriteLine("Enter a string: ");
    string str=Console.ReadLine();
    Console.WriteLine (isPallindromePermutation(str));
  }
}

// Sample output:-


Related Solutions

Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
C++ function to a string into enum function to a enum into a string check valid...
C++ function to a string into enum function to a enum into a string check valid / loop ( asking a couple of times until answer invaild) For example Enum Fruit ( APPLE, STRAWBERRY, BLUEBERRY, BLACKBERRY) output should be what is your favorit fruit? : strawberry you will have STRAWBERRY. what is your favorite fruite : peach invaild TIA
Write a function that takes a string as an argument checks whether it is a palindrome....
Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and L2=[] ....
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked...
C++ Question Write a code for :- public List Palindrome(); //Check whether a given singly linked list is palindrome or not. Input: a -> b-> NULL a -> b -> a-> NULL s -> a -> g -> a -> r-> NULL r -> a -> d -> a -> r-> NULL Output: not palindrome palindrome not palindrome palindrome Note:- Code should work on MS-Visual Studio 2017 and provide outputs with code
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Palindrome Javascript I am trying to write this javascript function to check if a number is...
Palindrome Javascript I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. This is the code i have, but it doesnt work. Code: let convertButton = document.getElementsByClassName("btn")[0]; let userInput = document.getElementById("number").value; let results = document.getElementById("result").value; convertButton.addEventListener("click", function (event) { event.preventDefault(); console.log(userInput); if (validatePalidrome(userInput)) document.getElementById("result").innerHTML = "true"; else document.getElementById("result").innerHTML = "false"; }); function validatePalidrome(numbers) { let...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a...
Palindrome Javascript - NUMBERS I am trying to write this javascript function to check if a number is Palindrome.. Palindrome means - a word, phrase, or sequence that reads the same backward as forward, e.g., 12321 This is the code i have, but it doesnt work. PLEASE MAKE SURE IT WORK BEFORE POST ANSWER, I GOT @ CODE THAT DID WORK BEFORE HTML JavaScript Palindrome Exercise rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css" /> Palindrome Enter a positive number: Is this a palindrome? No...
I wrote this program to check a string is palindrome or not. but in both cases,...
I wrote this program to check a string is palindrome or not. but in both cases, it gives me palindrome. could someone help me with this program? it has an error I couldn't find. when I run the code and give a string, in both cases it gives me palindrome. for example if I give Pop it says it is a palindrome but if I give Salima, it also says palindrome. #include<string> #include <iostream> using namespace std; class PString:public string...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A palindrome is a word, phrase, or sequence that reads the same backward as forward . Some examples you may try: “madam” redder “race car” Eva, Can I Stab Bats In A Cave? If the argument passed is not a string, invoke an exception or an assertion and state in a comment which one you have chosen and why.
VBA question, write a function check keyword in a string. Question: Function sentimentCalc(tweet As String) As...
VBA question, write a function check keyword in a string. Question: Function sentimentCalc(tweet As String) As Integer This function should check each word in the tweet and if the word exists as one of the keywords in the positive list or negative list it should impact the overall sentiment value. The positive list and negative list words exist in the keywords sheet. Access the keywords as ranges within your VBA code. The case of the word is inconsequential. For instance,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT