Skip to content

Commit c88cb67

Browse files
committed
🚀 03-Sep-2020
1 parent ff1a142 commit c88cb67

11 files changed

+754
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Given two equal-size strings s and t. In one step you can choose any character of t and replace it with another character.
2+
3+
Return the minimum number of steps to make t an anagram of s.
4+
5+
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
6+
7+
8+
9+
Example 1:
10+
11+
Input: s = "bab", t = "aba"
12+
Output: 1
13+
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
14+
Example 2:
15+
16+
Input: s = "leetcode", t = "practice"
17+
Output: 5
18+
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
19+
Example 3:
20+
21+
Input: s = "anagram", t = "mangaar"
22+
Output: 0
23+
Explanation: "anagram" and "mangaar" are anagrams.
24+
Example 4:
25+
26+
Input: s = "xxyyzz", t = "xxyyzz"
27+
Output: 0
28+
Example 5:
29+
30+
Input: s = "friend", t = "family"
31+
Output: 4
32+
33+
34+
Constraints:
35+
36+
1 <= s.length <= 50000
37+
s.length == t.length
38+
s and t contain lower-case English letters only.
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
// Using hash table
50+
51+
class Solution {
52+
public:
53+
int minSteps(string s, string t) {
54+
int slen=s.length();
55+
int tlen=t.length();
56+
57+
vector<int> freq(26, 0);
58+
59+
for(int i=0;i<slen;i++) freq[s[i]-'a']++;
60+
61+
int cnt=0;
62+
63+
for(int i=0;i<tlen;i++){
64+
if(freq[t[i]-'a']<=0){
65+
cnt++;
66+
} else freq[t[i]-'a']--;
67+
}
68+
69+
return cnt;
70+
71+
}
72+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Compare two version numbers version1 and version2.
2+
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.
3+
4+
You may assume that the version strings are non-empty and contain only digits and the . character.
5+
6+
The . character does not represent a decimal point and is used to separate number sequences.
7+
8+
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
9+
10+
You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.
11+
12+
13+
14+
Example 1:
15+
16+
Input: version1 = "0.1", version2 = "1.1"
17+
Output: -1
18+
Example 2:
19+
20+
Input: version1 = "1.0.1", version2 = "1"
21+
Output: 1
22+
Example 3:
23+
24+
Input: version1 = "7.5.2.4", version2 = "7.5.3"
25+
Output: -1
26+
Example 4:
27+
28+
Input: version1 = "1.01", version2 = "1.001"
29+
Output: 0
30+
Explanation: Ignoring leading zeroes, both “01and001" represent the same number “1”
31+
Example 5:
32+
33+
Input: version1 = "1.0", version2 = "1.0.0"
34+
Output: 0
35+
Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"
36+
37+
38+
Note:
39+
40+
Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
41+
Version strings do not start or end with dots, and they will not be two consecutive dots.
42+
43+
44+
45+
46+
47+
48+
49+
50+
class Solution {
51+
public:
52+
int compareVersion(string version1, string version2) {
53+
int n1=version1.length();
54+
int n2=version2.length();
55+
56+
int num1=0, num2=0;
57+
int i=0, j=0;
58+
while(i<n1 || j<n2){
59+
while(i< n1 && version1[i]!='.'){
60+
num1=(num1*10)+(version1[i]-'0');
61+
i++;
62+
}
63+
while(j<n2 && version2[j]!='.'){
64+
num2=(num2*10)+(version2[j]-'0');
65+
j++;
66+
}
67+
68+
if(num1>num2) return 1;
69+
if(num1<num2) return -1;
70+
71+
num1=0;
72+
num2=0;
73+
74+
i++;
75+
j++;
76+
}
77+
return 0;
78+
}
79+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
2+
3+
Example 1:
4+
5+
Input: nums = [1,2,3,1], k = 3, t = 0
6+
Output: true
7+
Example 2:
8+
9+
Input: nums = [1,0,1,1], k = 1, t = 2
10+
Output: true
11+
Example 3:
12+
13+
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
14+
Output: false
15+
Hide Hint #1
16+
Time complexity O(n logk) - This will give an indication that sorting is involved for k elements.
17+
Hide Hint #2
18+
Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is not necessary to sort next overlapping set of k numbers again.
19+
20+
21+
22+
23+
24+
25+
// O(n2)
26+
27+
class Solution {
28+
public:
29+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
30+
vector<vector<int>> v;
31+
32+
for(int i=0;i<nums.size();i++)
33+
{
34+
v.push_back({nums[i],i});
35+
}
36+
sort(v.begin(),v.end());
37+
for(int i=0;i<v.size();i++)
38+
{
39+
for(int j=i+1;j<v.size();j++)
40+
{
41+
if(v[j][0]>v[i][0]+t) break;
42+
if(abs(v[j][1]-v[i][1])<=k) return 1;
43+
}
44+
}
45+
return 0;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
2+
3+
Example 1:
4+
5+
Input: nums = [1,2,3,1], k = 3, t = 0
6+
Output: true
7+
Example 2:
8+
9+
Input: nums = [1,0,1,1], k = 1, t = 2
10+
Output: true
11+
Example 3:
12+
13+
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
14+
Output: false
15+
Hide Hint #1
16+
Time complexity O(n logk) - This will give an indication that sorting is involved for k elements.
17+
Hide Hint #2
18+
Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is not necessary to sort next overlapping set of k numbers again.
19+
20+
21+
22+
23+
24+
25+
// O(n2)
26+
27+
class Solution {
28+
public:
29+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
30+
vector<vector<int>> v;
31+
32+
for(int i=0;i<nums.size();i++)
33+
{
34+
v.push_back({nums[i],i});
35+
}
36+
sort(v.begin(),v.end());
37+
for(int i=0;i<v.size();i++)
38+
{
39+
for(int j=i+1;j<v.size();j++)
40+
{
41+
if(v[j][0]>v[i][0]+t) break;
42+
if(abs(v[j][1]-v[i][1])<=k) return 1;
43+
}
44+
}
45+
return 0;
46+
}
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Given two strings s and t , write a function to determine if t is an anagram of s.
2+
3+
Example 1:
4+
5+
Input: s = "anagram", t = "nagaram"
6+
Output: true
7+
Example 2:
8+
9+
Input: s = "rat", t = "car"
10+
Output: false
11+
Note:
12+
You may assume the string contains only lowercase alphabets.
13+
14+
Follow up:
15+
What if the inputs contain unicode characters? How would you adapt your solution to such case?
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
// Use hash table
26+
27+
class Solution {
28+
public:
29+
bool isAnagram(string s, string t) {
30+
vector<int> freq(256, 0);
31+
int tlen=t.length();
32+
int slen=s.length();
33+
34+
if(slen!=tlen) return false;
35+
36+
for(int i=0;i<tlen;i++)
37+
freq[t[i]]++;
38+
39+
for(int i=0;i<slen;i++){
40+
if(freq[s[i]]<=0) return false;
41+
freq[s[i]]--;
42+
}
43+
return true;
44+
}
45+
};
46+
47+
48+
49+
50+
51+
// Alter
52+
// Sorting : Sort the two anagrams and check if they are equal or not O(logN)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
The count-and-say sequence is the sequence of integers with the first five terms as following:
2+
3+
1. 1
4+
2. 11
5+
3. 21
6+
4. 1211
7+
5. 111221
8+
1 is read off as "one 1" or 11.
9+
11 is read off as "two 1s" or 21.
10+
21 is read off as "one 2, then one 1" or 1211.
11+
12+
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.
13+
You can do so recursively, in other words from the previous member read off the digits,
14+
counting the number of digits in groups of the same digit.
15+
16+
Note: Each term of the sequence of integers will be represented as a string.
17+
18+
19+
20+
Example 1:
21+
22+
Input: 1
23+
Output: "1"
24+
Explanation: This is the base case.
25+
Example 2:
26+
27+
Input: 4
28+
Output: "1211"
29+
Explanation: For n = 3 the term was "21" in which we have two groups "2" and "1", "2" can be read as "12"
30+
which means frequency = 1 and value = 2, the same way "1" is read as "11",
31+
so the answer is the concatenation of "12" and "11" which is "1211".
32+
33+
34+
35+
36+
37+
38+
39+
40+
// Iterative
41+
42+
class Solution {
43+
public:
44+
string countAndSay(int n) {
45+
if(n == 0) return "";
46+
string res = "1";
47+
while (--n) {
48+
string cur="";
49+
for (int i = 0; i < res.size(); i++) {
50+
int count = 1;
51+
while ((i + 1 < res.size()) && (res[i] == res[i + 1])){
52+
count++;
53+
i++;
54+
}
55+
cur += to_string(count) + res[i];
56+
}
57+
res = cur;
58+
}
59+
return res;
60+
}
61+
};
62+
63+
64+
65+
// Recursive
66+
67+
class Solution {
68+
public:
69+
string countAndSay(int n) {
70+
if(n == 1) return "1";
71+
if(n == 2) return "11";
72+
string s = countAndSay(n-1), res = "";
73+
int i = 0;
74+
while(i < s.size()) {
75+
int j = i;
76+
while(j < s.size() and s[j] == s[i]) j++;
77+
res += to_string(j - i) + s[i];
78+
i = j;
79+
}
80+
return res;
81+
}
82+
};

0 commit comments

Comments
 (0)