File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Anagrams
2
+ // Given an array of strings, return all groups of strings that are anagrams.
3
+ // Note: All inputs will be in lower-case.
4
+
Original file line number Diff line number Diff line change
1
+ // Length of Last Word
2
+
3
+ // method 1: NO STL
4
+ class Solution {
5
+ public:
6
+ int lengthOfLastWord (const char *s) {
7
+ int length = strlen (s);
8
+ if (length == 0 ) return 0 ;
9
+ int lengthofLast = 0 ;
10
+ // visit from back, add 1 to length when it does not meet with " "
11
+ // stop at " "
12
+ for (int i = length - 1 ; i >= 0 ; i--){
13
+ if (s[i] != ' ' ) lengthofLast ++;
14
+ else { // e.g "a ". we have to check the length is greater than 0
15
+ if (lengthofLast > 0 ) return lengthofLast;
16
+ }
17
+ }
18
+ return lengthofLast;
19
+
20
+ }
21
+ };
22
+
23
+ // method 2: STL
24
+ // find the first alpha and start from the alpha, find isnot alpha one.
25
+ class Solution {
26
+ public:
27
+ int lengthOfLastWord (const char *s) {
28
+ const string str (s);
29
+ auto first = find_if (str.rbegin (), str.rend (), ::isalpha);
30
+ auto last = find_if_not (first, str.rend (), ::isalpha);
31
+ return distance (first, last);
32
+ }
33
+ };
You can’t perform that action at this time.
0 commit comments