- Valid Anagram
- Topic
- Ideas and Answers
- Answer
- Valid palindrome
- Topic
- Ideas and Answers
- Answer
- Valid palindrome II
- Topic
- Ideas and Answers
- Answer
- Valid parentheses
- Topic
- Ideas and Answers
- Answer
- Valid Perfect Square
- Topic
- Ideas and Answers
- Answer
Note that the answer is simply that the code is written by someone else, correct, but not necessarily through testing (such as timeouts), but that they are unique, though most of them are better than mine.
1. Valid Anagram Topics
Given, strings s and t, write a function to determine if it is a anagram of S.
For example,
s = "Anagram", T = "Nagaram", return true.
s = "Rat", t = "Car", return false.
Note:
Assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain Unicode characters? How would adapt your solution to such case?
Ideas and Answers
I don't know what that means.
is s and T used the same character?
Set, huh?
It seems to require equal number of characters?
Dict Bai
1 ds,dt = {},{}2for in S:3 ds[w] = Ds.get (w,0 ) +14for in t:5 dt[w] = Dt.get (w,0) +16 return ds = = dt
Answer
Ah, I also want to use sorted to do ... But a flash and a forget?
return sorted (s) = = sorted (t)
return for in String.ascii_lowercase])
return collections. Counter (s) ==collections. Counter (t)
It's a different line of plan.
See someone say a dict can solve, think a bit yes.
# I wrote it. D = {} for in s: = D.get (w,0) +1 for in t: = D.get (w,0)-1 if not d[w]:del d[w] return not D
2. Valid palindrome Topics
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a Canal:panama" is a palindrome.
"Race a Car" is a palindrome.
Note:
Are you consider that the string might is empty? This was a good question to ask during a interview.
For the purpose of this problem, we define empty string as valid palindrome.
Ideas and Answers
I don't understand that example.
"A man, a plan, a Canal:panama" is a palindrome.
Why???
Oh, just ask for the letters to be symmetrical.
Judging if it's a letter, I remember a function.
for inch if i.isalnum ()] return n = = n[::-1]
Answer
Pointer scheme, did not consider to write so (because after all trouble)
def Ispalindrome (self, s): = 0, Len (s)-1 while L < r: when and not s[l].isalnum (): + = 1 while and not s[r].isalnum () :-= 1 if s[l].lower ()! = S[r].lower () :return False +=1; R-= 1 return True
3. Valid Palindrome II Topics
Given a non-empty string s, you could delete at the most one character. Judge Whether you can make it a palindrome.
Example 1:
Input: "ABA"
Output:true
Example 2:
Input: "ABCA"
Output:true
Explanation:you could delete the character ' C '.
Note:
The string would be only contain lowercase characters a-Z. The maximum length of the string is 50000.
Ideas and Answers
Lying trough, if each one is deleted by comparison ... Feels like it's going to expire.
No, compare palindrome first, make a mistake and consider the plan.
This is going to use the previous pointer scheme.
In dealing with the first error there is a problem, how to ensure that the one you deleted is right ... Feel like it's going to be completely compared.
defHuiwen (n,f): L,r= 0, Len (n)-1 whileL <r:ifn[l]!=N[r]:ifF:returnHuiwen (n[l+1:r+1],0)orHuiwen (n[l:r],0)Else: returnFalse L+ = 1R-= 1returnTruereturnHuiwen (s,1)
Because it's going to be a couple of times, so I just wrote a function.
Unfortunately, the speed is not.
Answer
Emmmm Why am I going to use a pointer?
1Rev = S[::-1]2 ifs = = Rev:returnTrue3L =Len (s)4 forIinchxrange (L):5 ifS[i]! =Rev[i]:6 returnS[i:l-i-1] = = Rev[i+1:l-i]orRev[i:l-i-1] = = s[i+1:l-i]7 returnFalse
Pretty much the plan.
1 def Validpalindrome (self, s): 2 i = 03while and s[i] = = s[-(i + 1)]: i + = 14 s = S[i:len (s)- I]5 returnor s[:-1] = = S[:-1][::-1]
4. Valid parentheses Topics
Given A string containing just the characters ' (', ') ', ' {', '} ', ' [' and '] ', determine if the input string is valid.
The brackets must close in the correct order, "()" and "() []{}" is all valid but "(]" and "([)]" is not.
Ideas and Answers
Stack?
How do I associate two parentheses in a dictionary?
Cannot find key based on values ....
D.items () What's wrong??
Looks like it can be removed, and there's a judgment back.
1stack=[]2b==")":"(","}":"{","]":"["}3 forNinchS:4 ifNinchd.values ():5 stack.append (n)6 elifNinchD.keys ():7 if notStackreturnFalse8x =Stack.pop ()9 ifX! =D[n]:Ten returnFalse One Else: A returnFalse - returnstack = = []
Speed is fine.
Answer
Almost (but shorter than mine)
1stack = []2Pairs = {'(':')','{':'}','[':']'}3 forCharinchS:4 ifCharinchPairs:5 stack.append (Pairs[char])6 Else:7 ifLen (stack) = = 0orStack.pop ()! =Char:8 returnFalse9 return notStack
5. Valid Perfect Square Topic
Given a positive integer num, write a function which returns True if Num is a perfect square else False.
Note:do not use any built-in library function such as sqrt.
Example 1:
Input:16
Returns:true
Example 2:
Input:14
Returns:false
Ideas and Answers
Does this number mean the sum of the squares of other integers?
Feel the need to search the judging method
Total square number equals 1+3+5+7+9+....+2n-1
Faster than the violent version.
1 N=12while num > 0:3 num-= n+n-14 n + = 15 return num = = 0
Other people have faster, estimates are not the same way
Answer
Emmm is one of the previous formulas, incredibly faster than mine.
1 def isperfectsquare (self, num): 2 x = num3 r = x4while r*r > x:5 R = (r + x/r)/26 return R*r = = X
Five practical Python case questions, very useful!