Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4461ef5

Browse files
.
1 parent 92ee7b3 commit 4461ef5

8 files changed

+201
-36
lines changed

‎Group_anagram.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* https://leetcode.com/problems/group-anagrams/ */
2+
3+
4+
class Solution {
5+
public:
6+
vector<vector<string>> groupAnagrams(vector<string>& strs) {
7+
vector<vector<string>> ans;
8+
unordered_map<string,vector<string>>m;
9+
for(string s:strs)
10+
{
11+
string temp=s;
12+
sort(temp.begin(),temp.end());
13+
m[temp].push_back(s);
14+
}
15+
16+
for(auto i:m)
17+
{
18+
ans.push_back(i.second);
19+
}
20+
return ans;
21+
22+
}
23+
};

‎Permutation_in_string.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://leetcode.com/problems/permutation-in-string/description/
2+
3+
class Solution {
4+
public:
5+
bool checkInclusion(string p, string s) {
6+
7+
int n=s.length();
8+
int m=p.length();
9+
vector<int>mt(26,0);
10+
vector<int>mp(26,0);
11+
if(m>n) return false;
12+
int count=0;
13+
for(int i=0;i<m;i++) //make 1st window of pattern size
14+
{
15+
mp[p[i]-'a']++;
16+
mt[s[i]-'a']++;
17+
}
18+
int left=0;
19+
int right=m-1;
20+
while(right<n)
21+
{
22+
if(mt==mp) return true;
23+
24+
right++;
25+
if(right!=n)
26+
{
27+
mt[s[right]-'a']++; //update the maping of window as window shift right side
28+
29+
mt[s[left++]-'a']--;
30+
}
31+
}
32+
33+
return false;
34+
}
35+
};

‎Remove_Palindrome_Subsequences.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* https://leetcode.com/problems/remove-palindromic-subsequences/description/ */
2+
3+
class Solution {
4+
public:
5+
int removePalindromeSub(string s) {
6+
int i=0;
7+
int j=s.length()-1;
8+
9+
while(i<j)
10+
{
11+
if(s[i]!=s[j]) return 2; // subsequence- remove all a then b if it is not palindrome 2 step.
12+
i++;
13+
j--;
14+
15+
}
16+
return 1;
17+
18+
}
19+
};

‎Repeated_DNA_Sequences.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/repeated-dna-sequences/
2+
3+
class Solution {
4+
public:
5+
vector<string> findRepeatedDnaSequences(string s) {
6+
vector<string>v;
7+
unordered_map<string,int>m;
8+
if(s.length()<=10) return v;
9+
for(int i=0;i<=s.size()-10;i++)
10+
{
11+
string str=s.substr(i,10);
12+
m[str]++;
13+
}
14+
for(auto it=m.begin();it!=m.end();it++)
15+
{
16+
if(it->second>1)
17+
{
18+
v.push_back(it->first);
19+
}
20+
}
21+
return v;
22+
}
23+
};

‎aa.cpp‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎anagram_occurence.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* https://practice.geeksforgeeks.org/problems/count-occurences-of-anagrams5839/1 */
2+
3+
int search(string p, string s) {
4+
// code here
5+
int n=s.length();
6+
int m=p.length();
7+
vector<int>mt(26,0);
8+
vector<int>mp(26,0);
9+
int count=0;
10+
for(int i=0;i<m;i++)
11+
{
12+
mp[p[i]-'a']++;
13+
mt[s[i]-'a']++;
14+
}
15+
int left=0;
16+
int right=m-1;
17+
while(right<n)
18+
{
19+
if(mt==mp) count++;
20+
21+
right++;
22+
if(right!=n)
23+
{
24+
mt[s[right]-'a']++;
25+
mt[s[left++]-'a']--;
26+
}
27+
}
28+
return count;
29+
}

‎longest_palindromecpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* https://leetcode.com/problems/longest-palindrome/ */
2+
3+
class Solution {
4+
public:
5+
int longestPalindrome(string s) {
6+
unordered_map<char,int>m;
7+
for(int i=0;i<s.length();i++)
8+
{
9+
m[s[i]]++;
10+
}
11+
int count=0;
12+
for(auto i:m)
13+
{
14+
if(i.second%2!=0)
15+
{
16+
count++;
17+
}
18+
}
19+
if(count>1) // if there is odd times appear char we can only fit one in the middle , rest of are even which can make palindrome , so total length-odd+1
20+
return s.length()-count+1;
21+
else
22+
return s.length(); //there is no odd number of times character then it is even number's string which is definitely palindrome
23+
}
24+
};

‎sort_the_people.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* https://leetcode.com/problems/sort-the-people/description/ */
2+
3+
/* STL */
4+
class Solution {
5+
public:
6+
7+
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
8+
map<int,string>m;
9+
for(int i=0;i<names.size();i++)
10+
{
11+
m[heights[i]]=names[i];
12+
13+
}
14+
vector<string>ans;
15+
// reverse(m.begin(),m.end());
16+
for(auto i=m.rbegin();i!=m.rend();i++)
17+
{
18+
ans.push_back(i->second);
19+
}
20+
return ans;
21+
}
22+
};
23+
24+
25+
/* using pair*/
26+
class Solution {
27+
public:
28+
29+
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
30+
vector<pair<int,string>>v;
31+
for(int i=0;i<names.size();i++)
32+
{
33+
v.push_back(make_pair(heights[i],names[i]));
34+
}
35+
sort(v.begin(),v.end());
36+
reverse(v.begin(),v.end());
37+
vector<string> ans;
38+
for(int i=0;i<names.size();i++)
39+
{
40+
ans.push_back(v[i].second);
41+
}
42+
for(auto i:v)
43+
{
44+
cout<<i.second<<" ";
45+
}
46+
return ans;
47+
}
48+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /