the longest palindrome string
Give a string containing uppercase and lowercase letters. Find out the length of the longest palindrome string made up of these letters.
The data is case sensitive, meaning that "Aa" is not considered a palindrome. Sample Example
given s = "ABCCCCDD" returns 7
One of the longest palindrome schemes that can be built is "DCCACCD". Algorithmic Solutions
This algorithm is a key-value pair relationship, a character can not constitute a palindrome, but when each word sizes at 2 o'clock, will produce palindrome pairs. At this time also to consider whether the parity of the same number of characters affect the length of the palindrome string. Here, the Java map is used to answer, with the character as the key name, the number of corresponding characters as the key value.
public class Solution {/* * @param s:a string which consists of lowercase or uppercase letters * @return: the length O f the longest palindromes that can be built */public int longestpalindrome (String s) {//write your code he
Re int index = 0;
if (S.length () ==0) return 0;
string[] _s = S.split ("");
Hashmap<string,integer>map = new hashmap<string,integer> ();
for (String str:_s) {Integer num = map.get (str);
Map.put (str,num==null?1:num+1);
} iterator<string> iter = Map.keyset (). Iterator ();
while (Iter.hasnext ()) {string key = (string) iter.next ();
if (Map.get (key) >=2&&map.get (key)%2==0) index = Index+map.get (key);
else if (Map.get (key) >=2&&map.get (key)%2!=0) index = index+map.get (key)-1;
} if ((S.length ()-index) >0) return index+1; Else
return index;
}
}
Strings homomorphism
Given strings S and T, determine if they are isomorphic.
The strings is isomorphic if the characters in S can is replaced to get t.
All occurrences of a character must is replaced with another character while preserving the order of characters. No, characters may map to the same character and a character may map to itself. Sample Example
Given s = "egg", t = "Add", return true.
Given s = "foo", t = "Bar", return false.
Given s = "paper", T = "title", Return True. algorithm
The purpose of this question is to determine whether the two strings are the same structure, it is obvious to use the key value of the relationship to judge
public class Solution {
/**
* @param s a string
* @param t a string
* @return True if the characters in S Can is replaced to get T or false
*
/public boolean isisomorphic (string s, String t) {
//Write Your code here
hashmap<character, character> record = new hashmap<> ();
hashset<character> repeat = new hashset<> ();
for (int i = 0;i<s.length (); i++) {
if (!record.containskey (S.charat (i))) {
if (Repeat.contains (T.charat (i )) {
return false;
}
Record.put (S.charat (i), T.charat (i));
Repeat.add (T.charat (i));
else{
if (Record.get (S.charat (i)! = T.charat (i)) {
return false;
}
}
}
return true;
}
}
First Position Unique Character
Given a string, find the first non-repeating character in it and return it ' s index. If it doesn ' t exist, return-1. Sample Example
Given s = "Lintcode", return 0.
Given s = "Lovelintcode", return 2. algorithm
Looking for an index with no duplicate characters, too simple, the code is as follows:
public class Solution {
/* * @param s:a string
* @return: it ' s index *
/public
int Firstuniqchar (stri ng s) {
//write your code here
int index =-1;
map<character,integer> map = new linkedhashmap<character,integer> ();
for (int i=0;i<s.length (); i++) {
Integer num = map.get (S.charat (i));
Map.put (S.charat (i), num==null?1:num+1);
}
Iterator<character> iter = Map.keyset (). Iterator ();
while (Iter.hasnext ()) {
Character cha = iter.next ();
if (Map.get (CHA) ==1) {
index = s.indexof (CHA);
break;
}
}
return index;
}
}
two array intersection (2)
NUMS1 = [1, 2, 2, 1], nums2 = [2, 2], returns [2, 2].
public class Solution {
/**
* @param nums1 a integer array
* @param nums2 an integer array
* @return an I Nteger Array
*
/public int[] intersection (int[] nums1, int[] nums2) {
list<integer> List = new Arraylis T<integer> ();
Arrays.sort (NUMS1);
Arrays.sort (NUMS2);
int i = 0, j = 0;
while (I < nums1.length && J < nums2.length) {
if (Nums1[i] < nums2[j]) {
i++;
} else if (nums1[i] = = Nums2[j]) {
list.add (nums1[i]);
i++;
j + +;
} else{
j + +;
}
}
Int[] inter = New int[list.size ()];
for (i = 0; i < inter.length; i++) {
Inter[i] = List.get (i);
}
return inter;
}
}
two array intersection (1)
Sample Example
NUMS1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
public class Solution {/** * @param nums1 an integer array * @param nums2 an integer array * @return an integer A Rray */public static int[] intersection (int[] nums1, int[] nums2) {//Write your code here set<i
nteger> set = new hashset<integer> ();
for (int m = 0; m < nums1.length; m++) Set.add (Nums1[m]);
list<integer> array = new linkedlist<integer> ();
for (int i = 0; i < nums2.length; i++) {if (Set.contains (Nums2[i])) {Array.add (nums2[i]);
}} int _index = 0;
set<integer> _set = new hashset<integer> (array);
int[] _array = new int[_set.size ()];
Iterator<integer> iter = _set.iterator ();
while (Iter.hasnext ()) {_array[_index]=iter.next (). Intvalue ();
_index = _index+1;
} return _array; }
}