chore(CPlusPlus): add longest common prefix and first unique character (#619)
parent
33313e347a
commit
79efa7a0dc
|
@ -115,6 +115,8 @@
|
|||
- [Print first letter of every word](Strings/print-first-letter.cpp)
|
||||
- [Display longest name in a string array](Strings/longest-name.cpp)
|
||||
- [Boyer Moore pattern search](Strings/Boyer_Moore.cpp)
|
||||
- [Longest common prefix](Strings/longest-common-prefix.cpp)
|
||||
- [First unique character in the string](Strings/first-unique-character.cpp)
|
||||
|
||||
## Trees
|
||||
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
Description: Given a string 's', return the index of the first unique character from the string. If not found, return -1
|
||||
|
||||
Time Complexity: O(n) where n is the length of the string
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
//function starts
|
||||
int firstUniqChar(string s)
|
||||
{
|
||||
//declare a map to store the frequency of the character
|
||||
map<char, int> m;
|
||||
//storing the character and its frequency in the map for every character in the string
|
||||
for (char c : s)
|
||||
{
|
||||
m[c]++;
|
||||
}
|
||||
//the frequency of the non-repreating or unique character will be 1
|
||||
//1 indicates that it had appeared only once in the string
|
||||
//since we want to search from the beginning of the string
|
||||
//use for loop from beginning of the string
|
||||
//to check if that character appeared for a single time
|
||||
//if yes, return the index
|
||||
for (int i = 0; i < s.size(); i++)
|
||||
{
|
||||
if (m[s[i]] == 1)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//main starts
|
||||
int main()
|
||||
{
|
||||
string s;
|
||||
cout << "Enter a string: \n";
|
||||
getline(cin, s);
|
||||
cout << firstUniqChar(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Example 1:
|
||||
|
||||
Input: s = "leetcode"
|
||||
Output: 0
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: s = "loveleetcode"
|
||||
Output: 2
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: s = "aabb"
|
||||
Output: -1
|
||||
*/
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
Description: Given two strings, return the longest common prefix from both of the strings.
|
||||
Longest common prefix means returning the starting characters which are common in both the strings.
|
||||
For e.g.,
|
||||
s1 = "hello"
|
||||
s2 ="heat"
|
||||
Longest common prefix will be "he"
|
||||
Because only two characters from the starting are common between them.
|
||||
|
||||
Time Complexity: O(n) where n is the length of the string
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
//function starts
|
||||
string lcp(string s1, string s2)
|
||||
{
|
||||
string ans = "";
|
||||
if (s1.size() == 0 || s2.size() == 0)
|
||||
{
|
||||
return ans;
|
||||
}
|
||||
//in 'm' variable, we are storing the smallest string's size from s1 and s2
|
||||
//because the prefix will always be at most the size of the smallest string
|
||||
int m = s1.size() < s2.size() ? s1.size() : s2.size();
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
//if both the starting characters are not same, then break from the for loop
|
||||
if (s1[i] != s2[i])
|
||||
{
|
||||
break;
|
||||
}
|
||||
//else if they are same, add the character in the answer string
|
||||
else
|
||||
{
|
||||
ans += s1[i];
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
//main starts
|
||||
int main()
|
||||
{
|
||||
string s1;
|
||||
string s2;
|
||||
cout << "Enter string-1 : \n";
|
||||
getline(cin, s1);
|
||||
cout << "Enter string-2: \n";
|
||||
getline(cin, s2);
|
||||
cout << "Longest common prefix is: " << lcp(s1, s2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Sample Input:
|
||||
string s1 = "flower"
|
||||
string s2 = "flight"
|
||||
|
||||
Output
|
||||
Longest common prefix is: fl
|
||||
*/
|
Loading…
Reference in New Issue