229 lines
6.4 KiB
JSON
229 lines
6.4 KiB
JSON
{
|
|
"palindrome-String ": {
|
|
"prefix": "palindrome",
|
|
"body": [
|
|
"bool ispalindrome(string s)",
|
|
"{",
|
|
" int n=s.size();",
|
|
" for(int i=0;i<n;i++) if(s[i]!=s[n-i-1]) return 0;",
|
|
" return 1;",
|
|
"}"
|
|
],
|
|
"description": "String "
|
|
},
|
|
"pangram-string": {
|
|
"prefix": "pangram",
|
|
"body": [
|
|
"bool ispangram(string s)",
|
|
"{",
|
|
" int n=s.size();",
|
|
" int f[26]={0}; ",
|
|
" for(int i=0;i<n;i++) ",
|
|
" {",
|
|
" if(s[i]>='a' && s[i]<='z') ",
|
|
" f[s[i]-97]++;",
|
|
" else",
|
|
" if(s[i]>='A' && s[i]<='Z') ",
|
|
" f[s[i]-65]++;",
|
|
" }",
|
|
" for(int i=0;i<26;i++)",
|
|
" if(f[i]==0) return 0;",
|
|
" return 1;",
|
|
"}"
|
|
],
|
|
"description": "pangram-string"
|
|
},
|
|
"anagram-strings": {
|
|
"prefix": "anagram",
|
|
"body": [
|
|
"bool isanagram(string a, string b)",
|
|
"{",
|
|
" if(a.size()!=b.size()) return 0;",
|
|
" int f[26]={0}; ",
|
|
" for(int i=0;i<a.size();i++) ",
|
|
" {",
|
|
" if(a[i]>='a' && a[i]<='z') ",
|
|
" f[a[i]-97]++;",
|
|
" else",
|
|
" if(a[i]>='A' && a[i]<='Z') ",
|
|
" f[a[i]-65]++;",
|
|
" }",
|
|
" for(int i=0;i<b.size();i++) ",
|
|
" {",
|
|
" if(b[i]>='a' && b[i]<='z') ",
|
|
" f[b[i]-97]--;",
|
|
" else",
|
|
" if(b[i]>='A' && b[i]<='Z') ",
|
|
" f[b[i]-65]--;",
|
|
" }",
|
|
" for(int i=0;i<26;i++)",
|
|
" if(f[i]!=0) return 0;",
|
|
" return 1;",
|
|
"}"
|
|
],
|
|
"description": "anagram-strings"
|
|
},
|
|
"string->decimal": {
|
|
"prefix": "strtodec",
|
|
"body": [
|
|
"long long int string_to_decimal(string s)",
|
|
" {",
|
|
" long long int ans = 0, p = 1;",
|
|
" for (int i = s.size() - 1; i >= 0; i--)",
|
|
" {",
|
|
" ans += p * (s[i] - '0');",
|
|
" p = p * 10;",
|
|
" }",
|
|
" return ans;",
|
|
" }"
|
|
],
|
|
"description": "string->decimal"
|
|
},
|
|
"decimal->string": {
|
|
"prefix": "dectostr",
|
|
"body": [
|
|
"string decimal_to_string(long long int n)",
|
|
" {",
|
|
" if (n == 0)",
|
|
" return 0;",
|
|
" string ans = \"\";",
|
|
" while (n > 0)",
|
|
" {",
|
|
" ans = ans + (char)((n % 10) + 48);",
|
|
" n = n / 10;",
|
|
" }",
|
|
" reverse(ans.begin(), ans.end());",
|
|
" return ans;",
|
|
" }"
|
|
],
|
|
"description": "decimal->string"
|
|
},
|
|
"occurences of pattern in a string ": {
|
|
"prefix": "kmp",
|
|
"body": [
|
|
"vector<int> kmp(string s,string t) //returns the indices of occurences of pattern t in string s",
|
|
"{",
|
|
" if(s.size()<t.size())",
|
|
" return {};",
|
|
" ",
|
|
" int n=s.size(),m=t.size();",
|
|
" int lps[m+1]={0};",
|
|
" int len = 0,i=1,j;",
|
|
" while (i < m) {",
|
|
" if (t[i] ==t[len]) lps[i++] = ++len;",
|
|
" else",
|
|
" {",
|
|
" if (len != 0) len = lps[len - 1];",
|
|
" else lps[i++] = 0;",
|
|
" ",
|
|
" }",
|
|
" }",
|
|
" vector<int>ans;",
|
|
" i=0;j=0;",
|
|
" while (i<n) {",
|
|
" if (t[j] ==s[i]) { j++; i++; }",
|
|
" ",
|
|
" if (j == m) {",
|
|
" ans.pb(i-j);",
|
|
" j=lps[j - 1];",
|
|
" }",
|
|
" else if (i < n && t[j]!=s[i]) {",
|
|
" ",
|
|
" if (j != 0) j = lps[j - 1];",
|
|
" else i++; }",
|
|
" }",
|
|
" return ans;",
|
|
"}"
|
|
],
|
|
"description": "occurences of pattern in a string "
|
|
},
|
|
"count of unique characters in a string": {
|
|
"prefix": "distict_characters",
|
|
"body": [
|
|
"int distinct_str(string s) {",
|
|
" sort(s.begin(), s.end());",
|
|
" return unique(s.begin(), s.end()) - s.begin();",
|
|
"}"
|
|
],
|
|
"description": "count of unique characters in a string"
|
|
},
|
|
|
|
"longest common subsequence of two strings": {
|
|
"prefix": "lc_subsequence",
|
|
"body": [
|
|
"string lc_subsequence(string a,string b)",
|
|
"{",
|
|
" string ans=\"\";",
|
|
" int n=a.size(),m=b.size(),i,j;",
|
|
"",
|
|
" int dp[n][m];",
|
|
" dp[0][0]= a[0]==b[0]?1:0;",
|
|
" for(i=1;i<n;i++) dp[i][0]= a[i]==b[0]?1:dp[i-1][0];",
|
|
" for(j=1;j<m;j++) dp[0][j]= a[0]==b[j]?1:dp[0][j-1];",
|
|
"",
|
|
" for(i=1;i<n;i++)",
|
|
" {",
|
|
" for(j=1;j<m;j++)",
|
|
" {",
|
|
" if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;",
|
|
" else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);",
|
|
" }",
|
|
" }",
|
|
" i = n-1, j = m-1;",
|
|
" while (i>=0 && j>=0)",
|
|
" {",
|
|
" if (a[i] == b[j])",
|
|
" {",
|
|
" ans+=a[i]; ",
|
|
" i--; j--;",
|
|
" }",
|
|
" else if (dp[i-1][j] > dp[i][j-1]) i--;",
|
|
" else j--;",
|
|
" }",
|
|
" reverse(ans.begin(),ans.end());",
|
|
" return ans;",
|
|
"}"
|
|
],
|
|
"description": "longest common subsequence of two strings"
|
|
},
|
|
"longest common substring of two strings": {
|
|
"prefix": "lc_substring",
|
|
"body": [
|
|
"string lc_substring(string a,string b)",
|
|
"{",
|
|
" string ans=\"\";",
|
|
" int n=a.size(),m=b.size(),i,j;",
|
|
" if(n==0 || m==0) return \"\" ;",
|
|
" int dp[n][m];",
|
|
" dp[0][0]= a[0]==b[0]?1:0;",
|
|
" for(i=1;i<n;i++) dp[i][0]= a[i]==b[0]?1:0;",
|
|
" for(j=1;j<m;j++) dp[0][j]= a[0]==b[j]?1:0;",
|
|
" int len=0,row,col;",
|
|
" for(i=1;i<n;i++)",
|
|
" {",
|
|
" for(j=1;j<m;j++)",
|
|
" {",
|
|
" if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;",
|
|
" else dp[i][j]= 0;",
|
|
"",
|
|
" if (len < dp[i][j]) {",
|
|
" len = dp[i][j];",
|
|
" row = i;",
|
|
" col = j;",
|
|
" }",
|
|
" }",
|
|
" }",
|
|
" if(len==0) return \"\";",
|
|
" while (dp[row][col] != 0) {",
|
|
" ans+=a[row];",
|
|
" row--;",
|
|
" col--;",
|
|
" }",
|
|
" reverse(ans.begin(),ans.end());",
|
|
" return ans;",
|
|
"}"
|
|
],
|
|
"description": "longest common substring of two strings"
|
|
}
|
|
}
|