Title Description:
Write a function anagram(s, t) to determine whether two strings can be changed into the same string by changing the order of the letters.
Problem Solving Ideas:
C + +: The thought of introducing a hash, the problem is solved.
C + + Code:
Class Solution {
Public
/**
* @param s:the First string
* @param b:the Second string
* @return True or False
*/
BOOL Anagram (string s, String t) {
Write your code here
int dic[58];
for (int i = 0; i <; i++)
Dic[i] = 0;
for (int i = 0; i < s.size (); i++)
{
if (s[i] = = ")
Continue
int index = s[i]-' A ';
dic[index]++;
}
for (int i = 0; i < t.size (); i++)
{
if (t[i] = = ")
Continue
int index = t[i]-' A ';
dic[index]--;
}
for (int i = 0; i <; i++)
{
if (i==57 && dic[i]==0)
return true;
if (dic[i] = = 0)
Continue
Else
return false;
}
}
};
Python: The Python List () method and the sort () method can be used to solve this problem successfully.
Python Code:
Class Solution:
"""
@param s:the First String
@param b:the Second String
@return True or False
"""
def anagram (self, S, T):
# Write your code here
A = List (s)
b = List (t)
A.sort ()
B.sort ()
If a = = B:
Return True
Else
Return False
Lintcode Diary (a)--two strings are inflection words (C++,python)