File tree 9 files changed +230
-0
lines changed
9 files changed +230
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ string countAndSay (int n) {
4
+ vector<string> ans (n + 1 );
5
+
6
+ ans[1 ] = " 1" ;
7
+ if (n == 1 )
8
+ return " 1" ;
9
+
10
+ for (int i = 2 ; i <= n; i++){
11
+ string prev = ans[i-1 ];
12
+
13
+ int l = 0 ;
14
+ int r = 0 ;
15
+ string temp = " " ;
16
+ int x = prev.size ();
17
+ cout << x << endl;
18
+
19
+ while (r < x){
20
+ if (prev[r] == prev[l])
21
+ r++;
22
+ else {
23
+ temp += to_string (r-l);
24
+ temp += to_string (prev[l] - ' 0' );
25
+ l = r;
26
+ }
27
+
28
+ }
29
+
30
+ temp += to_string (r-l);
31
+ temp += to_string (prev[l] - ' 0' );
32
+
33
+ ans[i] = temp;
34
+
35
+ }
36
+
37
+ return ans[n];
38
+ }
39
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int firstUniqChar (string s) {
4
+ map<int ,int > mp;
5
+ for (auto c: s)
6
+ mp[c]++;
7
+
8
+ int n = s.length ();
9
+ for (int i = 0 ; i < n; i++)
10
+ if (mp[s[i]] == 1 )
11
+ return i;
12
+
13
+ return -1 ;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+
4
+ string LCP (string a, string b){
5
+ int i = 0 , j = 0 ;
6
+ while (i < (int )a.length () and j < (int )b.length ()){
7
+ if (a[i] == b[j]){
8
+ i++;
9
+ j++;
10
+ }else {
11
+ return a.substr (0 , i);
12
+ }
13
+ }
14
+
15
+ return a.substr (0 , i);
16
+
17
+ }
18
+
19
+ string longestCommonPrefix (vector<string>& strs) {
20
+ int n = strs.size ();
21
+ if (n == 0 )
22
+ return " " ;
23
+
24
+ string ans = strs[0 ];
25
+ for (auto str: strs){
26
+ ans = LCP (ans, str);
27
+ }
28
+ return ans;
29
+ }
30
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int reverse (int x) {
4
+ long long ans = 0 ;
5
+
6
+ while (x){
7
+
8
+ ans = ans * 10 + x % 10 ;
9
+ x = x/10 ;
10
+
11
+ if (ans > INT_MAX or ans < INT_MIN)
12
+ return 0 ;
13
+
14
+ }
15
+
16
+ return ans;
17
+ }
18
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ void reverseString (vector<char >& s) {
4
+ int n = s.size ();
5
+ int i = 0 , j = n-1 ;
6
+
7
+ while (i < j){
8
+ swap (s[i], s[j]);
9
+ i++;
10
+ j--;
11
+ }
12
+
13
+
14
+
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int myAtoi (string str) {
4
+ int n = str.length ();
5
+ int i = 0 ;
6
+ bool neg = false ;
7
+ while (str[i] == ' ' )
8
+ i++;
9
+
10
+ if (str[i] == ' -' ){
11
+ neg = true ;
12
+ i++;
13
+ }
14
+ else if (str[i] == ' +' ){
15
+ neg = false ;
16
+ i++;
17
+ }
18
+
19
+ if (!isdigit (str[i]))
20
+ return 0 ;
21
+
22
+ long long ans = 0 ;
23
+ while (i < n){
24
+ if (!isdigit (str[i]))
25
+ break ;
26
+ ans = ans * 10 + str[i] - ' 0' ;
27
+ i++;
28
+
29
+ ans = (neg ? -ans : ans);
30
+ if (ans < INT_MIN)
31
+ return INT_MIN;
32
+ if (ans > INT_MAX)
33
+ return INT_MAX;
34
+
35
+ ans = abs (ans);
36
+ }
37
+
38
+ ans = (neg ? -ans : ans);
39
+
40
+
41
+ return ans;
42
+
43
+
44
+ }
45
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isAnagram (string s, string t) {
4
+ map<int ,int > mp;
5
+ for (auto c: s)
6
+ mp[c]++;
7
+ for (auto c: t)
8
+ mp[c]--;
9
+
10
+ for (auto x: mp)
11
+ if (x.second != 0 )
12
+ return false ;
13
+ return true ;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isPalindrome (string s) {
4
+ int n = s.length ();
5
+ if (n == 0 or n == 1 )
6
+ return true ;
7
+
8
+ int i = 0 , j = n-1 ;
9
+ while (i < j){
10
+ if (!isalnum (s[i])){
11
+ i++;
12
+ continue ;
13
+ }
14
+ if (!isalnum (s[j])){
15
+ j--;
16
+ continue ;
17
+ }
18
+
19
+ if (tolower (s[i]) != tolower (s[j]))
20
+ return false ;
21
+ i++;
22
+ j--;
23
+
24
+ }
25
+
26
+ return true ;
27
+ }
28
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int strStr (string haystack, string needle) {
4
+ int k = needle.size ();
5
+ if (k == 0 )
6
+ return 0 ;
7
+ for (int i = 0 ; i <= (int )haystack.size () - (int )needle.size (); i++){
8
+ bool found = true ;
9
+
10
+ for (int j = 0 ; j < (int )needle.size (); j++){
11
+ if (haystack[i+j] != needle[j]){
12
+ found = false ;
13
+ break ;
14
+ }
15
+ }
16
+
17
+ if (found)
18
+ return i;
19
+
20
+ }
21
+
22
+ return -1 ;
23
+ }
24
+ };
You can’t perform that action at this time.
0 commit comments