------ count no of valleys------------
int countingValleys(int steps, string path) {
int count=0;
int sum=0;
unordered_map<char,int>mpp={{'D',-1},{'U',1}};
for(int i=0;i<[Link]();i++){
if(sum==0 && sum+mpp[path[i]]<0){
count++;
}
sum+=mpp[path[i]];
}
return count;
}
---------capital last and first of each word--------
void capital(string &s1){
for(int i=0;i<[Link]();i++){
if(s1[i]>='a' && s1[i]<'z'){
if(i==0 || i==[Link]()-1){
s1[i]=s1[i]-32;
}else if(s1[i+1]==' ' || s1[i-1]==' '){
s1[i]=s1[i]-32;
}
}
}
}
int main() {
// Write C++ code here
string input1;
getline(cin,input1);
capital(input1);
cout<<input1;
return 0;
}
----------largest and smallest word---------
#include <bits/stdc++.h>
using namespace std;
void capital(string &s1, string &maxword, string &minword) {
vector<string> ans;
string temp = "";
// Splitting words while handling multiple spaces
for (int i = 0; i < [Link](); i++) {
if (s1[i] == ' ') {
if (![Link]()) { // Avoid empty words
ans.push_back(temp);
temp = "";
}
} else {
temp += s1[i];
}
}
if (![Link]()) { // Add the last word
ans.push_back(temp);
}
// If no words are found, return
if ([Link]()) {
cout << "No words found in the input.\n";
return;
}
// Initialize min and max lengths properly
int max_length = 0;
int minlength = INT_MAX;
for (int i = 0; i < [Link](); i++) {
if (ans[i].size() > max_length) {
max_length = ans[i].size();
maxword = ans[i];
cout << "Updated max_length: " << max_length << " (Word: " << maxword
<< ")\n";
}
if (ans[i].size() < minlength) {
minlength = ans[i].size();
minword = ans[i];
cout << "Updated minlength: " << minlength << " (Word: " << minword <<
")\n";
}
}
}
--------remove dots and reverse the words---------------
string reverse(string s1) {
[Link](0, s1.find_first_not_of('.'));
[Link](s1.find_last_not_of('.') + 1);
vector<string>ans;
string temp="";
for(int i=0;i<[Link]();i++){
if(s1[i]=='.'){
if([Link]()!=0){
ans.push_back(temp);
temp="";
}
}else{
temp+=s1[i];
}
}
if([Link]()!=0){
ans.push_back(temp);
}
reverse([Link](),[Link]());
temp="";
for(int i=0;i<[Link]()-1;i++){
temp+=ans[i]+".";
}
temp+=ans[[Link]()-1];
return temp;
Examples:
Input: str = “[Link]”
Output: str = “[Link].i”
Explanation:The words in the input string are reversed while maintaining the dots
as separators, resulting in "[Link].i".
Input: str = ”..geeks..[Link].”
Output: str = “[Link]”
Input: str = “…home……”
Output: str = “home”
-------first occurence--------
int firstOccurence(string &txt, string &pat) {
// Your code here
return [Link](pat);
-----------lexicographically----------
If string is empty, we return ‘a’. If string contains all characters as ‘z’, we
append ‘a’ at the end. Otherwise we find first character from end which is not z
and increment it.
Input : raavz
Output : raawz
Since we can't increase last character,
we increment previous character.
Input : zzz
Output : zzza
string lexicography(string s1) {
bool done=false;
for(int i=[Link]()-1;i>=0;i--){
if(s1[i]=='z'){
continue;
}else{
s1[i]=s1[i]+1;
done=true;
break;
}
}
if(!done){
s1+='a';
}
return s1;
}
-------------Remove brackets from an algebraic string containing + and –
operators------------
Input : "(a-(b+c)+d)"
Output : "a-b-c+d"
#include <bits/stdc++.h>
using namespace std;
string simplify(string s) {
stack<int> st; // Stack to store signs
[Link](1); // Initial sign is '+'
string result = "";
int i = 0;
while (i < [Link]()) {
if (s[i] == '+') {
// Use the current sign
if ([Link]() == 1) {
result += '+';
} else {
result += '-';
}
i++;
} else if (s[i] == '-') {
// Flip the current sign
if ([Link]() == 1) {
result += '-';
} else {
result += '+';
}
i++;
} else if (s[i] == '(') {
// Push the current sign onto the stack
if (i > 0 && s[i - 1] == '-') {
[Link](-[Link]()); // Flip the sign for nested parentheses
} else {
[Link]([Link]()); // Keep the same sign
}
i++;
} else if (s[i] == ')') {
// Pop the sign from the stack
[Link]();
i++;
} else {
// Append the character to the result
result += s[i];
i++;
}
}
return result;
}
-----------Remove Outermost Parentheses-----------
Input: s = "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
string removeOuterParentheses(string s) {
int counter=0;
//+1->opening bracket
//-1-->closing bracket
string result="";
for(int i=0;i<[Link]();i++){
if(s[i]=='(' && counter!=0){
result+=s[i];
counter++;
}else if(s[i]==')' && counter-1!=0){
result+=s[i];
counter--;
}else if(s[i]=='(' && counter==0){
counter++;
}else{
counter--;
}
}
return result;
}
------------------largest odd number---------------------
Input: num = "35427"
Output: "35427"
Explanation: "35427" is already an odd number.
string largestOddNumber(string num) {
int odd_idx=-1;
for(int i=[Link]()-1;i>=0;i--){
if((num[i]-'0')%2!=0){
odd_idx=i;
break;
}
}
if(odd_idx!=-1){
return [Link](0,odd_idx+1);
}
return "";
}
------------------common prefix----------------------------
string commonprefix(vector<string>&nums) {
string ans="";
sort([Link](),[Link]());
int n=[Link]();
string low=nums[0];
string high=nums[n-1];
for(int i=0;i<[Link]();i++){
if(low[i]!=high[i]){
break;
}
ans+=low[i];
}
return ans;
}
-------------------isomprphic string -------------------------
using one map
bool isIsomorphic(string s, string t) {
unordered_map<char, int> m1;
if([Link]()!=[Link]())return false;
for(int i=0;i<[Link]();i++){
if([Link](s[i])==[Link]()){
for (auto& pair : m1) {
if ([Link] == t[i]) {
return false;
}
}
m1[s[i]]=t[i];
}else if([Link](s[i])!=[Link]()){
if(t[i]!=m1[s[i]]){
return false;
}else{
continue;
}
}
}
return true;
}
using two maps
bool isIsomorphic(string s, string t) {
map<char,char>m1,m2;
for(int i=0;i<[Link]();i++){
if(m1[s[i]]&& m1[s[i]]!=t[i])return false;
if(m2[t[i]]&& m2[t[i]]!=s[i])return false;
m1[s[i]]=t[i];
m2[t[i]]=s[i];
}
return true;
}
---------------------Rotate string of each other------------------
Input: s = "abcde", goal = "cdeab"
Output: true
public boolean rotateString(String s, String goal) {
if ([Link]() != [Link]()) {
return false;
}
return (s + s).contains(goal);
}
--------------------Valid Anagram--------------------------------
there frequency will be the same
bool isAnagram(string s, string t) {
if([Link]()!=[Link]())return false;
unordered_map<char,int>mpp;
for(int i=0;i<[Link]();i++){
mpp[s[i]]++;
mpp[t[i]]--;
}
for(auto & it:mpp){
if([Link]>0){
return false;
}
}
return true;
}
--------------------sort by frequency---------------------
using map
static bool cmp(pair<char,int>&a,pair<char,int>&b){
if([Link]>[Link])return true;
else if([Link]==[Link]){
return [Link]<[Link];
}
return false;
}
string frequencySort(string s) {
map<char,int>mpp;
for(int i=0;i<[Link]();i++){
mpp[s[i]]++;
}
vector<pair<char, int>> v;
for (auto &it : mpp) {
v.push_back({[Link], [Link]});
}
sort([Link](),[Link](),cmp);
string c;
for (int i = 0; i < [Link](); i++) {
while (v[i].second--) {
c.push_back(v[i].first);
}
}
return c;
}
};
another approach
count array int count[256]={0}
-----------------------------Maximum Nesting Depth of the
Parentheses---------------------------
Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3
class Solution {
public:
int maxDepth(string s) {
int maxi=0;
int counter=0;
for(int i=0;i<[Link]();i++){
if(s[i]=='('){
counter++;
maxi=max(maxi,counter);
}else{
counter--;
}
}
return maxi;
}
};
----------------------------integer to roman--------------------------
string intToRoman(int num) {
vector<pair<int, string>> values = {
{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"},
{100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"},
{10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}
};
string result = "";
for (auto& it : values) {
int value = [Link];
string symbol = [Link];
while (num >= value) {
result += symbol;
num -= value;
}
}
return result;
------------------------roman to integer--------------------------------
int romanToInt(string s) {
int ans=0;
unordered_map <char,int> mp{
{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
for(int i=0;i<[Link]();i++){
if(mp[s[i]]<mp[s[i+1]]){
//for cases such as IV,CM, XL, etc...
ans=ans-mp[s[i]];
}
else{
ans=ans+mp[s[i]];
}
}
return ans;
}
------------------string to integer---------------------------------
class Solution {
public:
int myAtoi(string s) {
int n=[Link]();
int i=0;
//remove leading zeros
while (i < n && s[i] == ' ') {
i++;
}
//check the sign
int sign=1;
if (s[i] == '-') { sign = -1; i++; }
else if (s[i] == '+') i++;
long res=0;
while(i<[Link]() && isdigit(s[i])){
res=res*10+(s[i]-'0');
if(sign*res>INT_MAX)return INT_MAX;
if(sign*res<INT_MIN)return INT_MIN;
i++;
}
return (int)(sign*res);
}
};
---------------------------longest pallindrome substr-----------------------
string longestPalindrome(string s) {
//INTUTION::
// from any point move low poiter leftwise and right pointer rightwise and
check equal or not
string ans = "";
for (int i = 0; i < [Link](); i++) {
// Odd length
int low = i;
int high = i;
while (low >= 0 && high < [Link]() && s[low] == s[high]) {
low--;
high++;
}
string palindromeOdd = [Link](low + 1, high - low - 1);
if ([Link]() > [Link]()) {
ans = palindromeOdd;
}
// Even length
low = i;
high = i + 1;
while (low >= 0 && high < [Link]() && s[low] == s[high]) {
low--;
high++;
}
string palindromeEven = [Link](low + 1, high - low - 1);
if ([Link]() > [Link]()) {
ans = palindromeEven;
}
}
return ans;
}
};
------------------------------------sum of beuty of
substring----------------------------------
The beauty of a string is the difference in frequencies between the most frequent
and least frequent characters.
For example, the beauty of "abaacc" is 3 - 1 = 2.
Input: s = "aabcb"
Output: 5
Explanation: The substrings with non-zero beauty are
["aab","aabc","aabcb","abcb","bcb"], each with beauty equal to 1.
class Solution {
public:
int beautySum(string s) {
int total_beuty=0;
for(int i=0;i<[Link]();i++){
vector<int>freq(26,0);
for(int j=i;j<[Link]();j++){
freq[s[j]-'a']++;
int maxi=*max_element([Link](),[Link]());
int mini=INT_MAX;
for(int f:freq){
if(f>0){
mini=min(f,mini);
}
}
total_beuty+=(maxi-mini);
}
}
return total_beuty;
}
};