palindrome

Palindrome Recursion Program

 ̄綄美尐妖づ 提交于 2019-12-08 10:56:18
问题 public static boolean palindrome(String input, int i, int j) { if (i >= j) return true; if (input.charAt(i) == input.charAt(j)) { i++; j--; palindrome(input, i, j); } else if (input.charAt(i) != input.charAt(j)) return false; } My Java platform (eclipse) won't accept this code as working, due to a "lack of return type." Now I know in proper coding ediquite, it's better to use only one return value, but when it comes to recursion, this is somewhat new to me. How can I go about doing this? If I

Counting palindromes in a text file

匆匆过客 提交于 2019-12-08 09:01:30
问题 Having followed this thread BASH Finding palindromes in a .txt file I can't figure out what am I doing wrong with my script. #!/bin/bash search() { tr -d '[[:punct:][:digit:]@]' \ | sed -E -e '/^(.)\1+$/d' \ | tr -s '[[:space:]]' \ | tr '[[:space:]]' '\n' } search "$1" paste <(search <"$1") <(search < "$1" | rev) \ | awk '$1 == $2 && (length($1) >=3) { print $1 }' \ | sort | uniq -c All im getting from this script is output of the whole text file. I want to only output palindromes >=3 and

What's the time complexity of this algorithm for Palindrome Partitioning?

懵懂的女人 提交于 2019-12-08 08:50:58
问题 Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Personally I think, the time complexity is O(n^n), n is the length of the given string. Thank you Dan Roche , the tight time complexity = O(n* (2^n)), check details below. #include <vector> using namespace std; class Solution { public: vector<vector<string>> partition(string s) { vector<vector<string>> list; vector<string> subList;

Finding a String Palindrome with a recursive function

▼魔方 西西 提交于 2019-12-08 08:14:41
问题 I am trying to write a recursive function that will determine if a string is a palindrome. Here is what I have so far: int main() { string word = "madam"; if (palindrome(word) == true) cout << "word is a palindrome!" << endl; else cout << "word is not a palindrome..." << endl; return 0; } bool palindrome(string word) { int length = word.length(); string first = word.substr(0,1); string last = word.substr((length - 1), 1); if (first == last) { word = word.substr((0 + 1), (length - 2)); cout <<

python and palindromes

最后都变了- 提交于 2019-12-08 03:34:44
问题 i recently wrote a method to cycle through /usr/share/dict/words and return a list of palindromes using my ispalindrome(x) method here's some of the code...what's wrong with it? it just stalls for 10 minutes and then returns a list of all the words in the file def reverse(a): return a[::-1] def ispalindrome(a): b = reverse(a) if b.lower() == a.lower(): return True else: return False wl = open('/usr/share/dict/words', 'r') wordlist = wl.readlines() wl.close() for x in wordlist: if not

What's the time complexity of this algorithm for Palindrome Partitioning?

↘锁芯ラ 提交于 2019-12-07 08:39:31
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Personally I think, the time complexity is O(n^n), n is the length of the given string. Thank you Dan Roche , the tight time complexity = O(n* (2^n)), check details below. #include <vector> using namespace std; class Solution { public: vector<vector<string>> partition(string s) { vector<vector<string>> list; vector<string> subList; // Input validation. if (s.length() <= 1) { subList.push_back(s); list.push_back(subList); return list

Prolog - Palindrome Functor

别等时光非礼了梦想. 提交于 2019-12-07 04:46:58
问题 I am trying to write a predicate palindrome/1 in Prolog that is true if and only if its list input consists of a palindromic list. for example: ?- palindrome([1,2,3,4,5,4,3,2,1]). is true. Any ideas or solutions? 回答1: A palindrome list is a list which reads the same backwards, so you can reverse the list to check whether it yields the same list: palindrome(L):- reverse(L, L). 回答2: Looks that everybody is voting for a reverse/2 based solution. I guess you guys have a reverse/2 solution in mind

Longest palindromic substring and suffix trie

a 夏天 提交于 2019-12-06 13:44:32
I was Googling about a rather well-known problem, namely: the longest palindromic substring I have found links that recommend suffix tries as a good solution to the problem. Example SO and Algos The approach is (as I understand it) e.g. for a string S create Sr (which is S reversed) and then create a generalized suffix trie. Then find the longest common sustring of S and Sr which is the path from the root to the deepest node that belongs both to S and Sr . So the solution using the suffix tries approach essentially reduces to Find the longest common substring problem. My question is the

How do you print out a palindrome with certain characters removed using arrays?

狂风中的少年 提交于 2019-12-06 13:26:53
My program is supposed to test for a palindrome and then print it out in reverse but without characters like '!', ', or '?'. So the entered input "madam I'm adam" would output "madamimadam" with no capitalization, blanks, or punctuation. I was able to write the program but how do you do that other part to take away those characters/capitalization? Also for my array, when the program runs it outputs a bunch of odd characters in between the palindrome and the palindrome in reverse when it prints out. I believe this is because the array is filling in the extra character spaces so how would I fix

python and palindromes

独自空忆成欢 提交于 2019-12-06 12:36:13
i recently wrote a method to cycle through /usr/share/dict/words and return a list of palindromes using my ispalindrome(x) method here's some of the code...what's wrong with it? it just stalls for 10 minutes and then returns a list of all the words in the file def reverse(a): return a[::-1] def ispalindrome(a): b = reverse(a) if b.lower() == a.lower(): return True else: return False wl = open('/usr/share/dict/words', 'r') wordlist = wl.readlines() wl.close() for x in wordlist: if not ispalindrome(x): wordlist.remove(x) print wordlist user225312 wordlist = wl.readlines() When you do this, there