String Programming Exercises (C++)

ex1 string transformation

describe

For a string of length n, we need to transform it a bit.

First, this string contains some spaces, just like "Hello World", and then what we need to do is to reverse the order of words separated by spaces in this string, and reverse the case of each character at the same time.

For example, "Hello World" becomes "wORLD hELLO" after deformation.

Data range: 1≤n≤ 10^{6} , the string includes uppercase English letters, lowercase English letters, and spaces.

Advanced: space complexity O(n), time complexity O(n)

Enter a description:

Given a string s and its length n(1 ≤ n ≤ 10^{6})

Return value description:

Please return the transformed string. The topic ensures that the given string consists of uppercase and lowercase letters and spaces.

 Idea description

There are two requirements for strings in this question:

1. Swap uppercase and lowercase letters

You can convert a lowercase letter s to an uppercase letter by performing s-'a'+'A' on the character s, and an uppercase letter s to a lowercase letter by s-'A'+'a'

for(int i=0;i<n;i++){
    if(s[i]<='Z'&&s[i]>='A')//大写变小写
        res+=s[i]-'A'+'a';
    else if(s[i]>='a'&&s[i]<='z')//小写变大写
        res+=s[i]-'a'+'A';
    else res+=s[i];//空格照抄
}

2. Reverse order of words inside letters

You can use the C++ reverse function reverse() to operate in two steps:

First reverse the entire string, for example: as bc can be changed to cb sa;

Then perform the string after the operation, and then reverse the sequence with spaces as the separation unit, such as: cb sa can be changed to bc as;

Then the first thing is to "recognize" each word, such as "cb" and "sa", you can record the initial position of the string through a for loop, and then find the end position of the word through a while loop, that is, a space Before.

for(int i=0;i<n;i++){
    int j=i;//二次反转的起始位置
    while(j<n&&res[j]!=' ')//二次反转的终止位置
        j++;
    reverse(res.begin()+i, res.begin()+j);//二次反转
    i=j;
}

Reference Code

#include <algorithm>
#include <string>
class Solution {
public:
   
    string trans(string s, int n) {
        // write code here
        if(n==0)
            return s;
        string res;
        for(int i=0;i<n;i++){
            if(s[i]<='Z'&&s[i]>='A')//大写变小写
                res+=s[i]-'A'+'a';
            else if(s[i]>='a'&&s[i]<='z')//小写变大写
                res+=s[i]-'a'+'A';
            else res+=s[i];//空格照抄
        }
        reverse(res.begin(),res.end());//整个反转
        for(int i=0;i<n;i++){
            int j=i;//二次反转的起始位置
            while(j<n&&res[j]!=' ')//二次反转的终止位置
                j++;
            reverse(res.begin()+i, res.begin()+j);//二次反转
            i=j;
        }
        return res;

    }
};

ex2 longest common prefix

describe

Given a string array strs of size n, which contains n strings, write a function to find the longest common prefix in the string array and return this common prefix.

Data range: 0≤n≤5000, 0≤len(strs_{i}​ ) ≤5000

Advanced: Space complexity O(1), time complexity O(n∗len)

example

Idea introduction

vertical traversal 

Starting from the first string, compare the first character of the first string with each of the following strings. If the characters in this column are the same, then compare the characters in the second column until you find a different character or It is a certain group of strings that have come to an end.

for(int i=0;i<strs[0].size();i++){//比较第一个字符串的每一个字符
    for(int j=1;j<strs.size();j++){//纵向比较字符串
        if(strs[0][i]!=strs[j][i]||i==strs[j].size()){//比较后面的每一个字符串
            return strs[0].substr(0,i);//若字符不相同或者长度为最小则返回最长公共前缀
        }
    }
}

Here you need to use the string segmentation function substr(), the function syntax is as follows:

string substr (size_t pos, size_t len) const;

Wherein, pos represents the starting position of the substring to be intercepted, and len represents the length of the substring to be intercepted.

There are also some other formats:

substr(string str, int a, int b);

str is the string to be intercepted
a the starting position of the intercepted string
b the length of the string to be intercepted

substr(string str, int a) ;

a can be understood as intercepting all subsequent strings starting from the ath character.

s.substr(pos, n)

Returns a string containing a copy of n characters starting from pos in s (the default value of pos is 0, and the default value of n is s.size() - pos, that is, if no parameter is added, the entire s will be copied by default). If pos
is If the value exceeds the size of the string, the substr function will throw an out_of_range exception; if the value of pos+n exceeds the size of the string, substr will adjust the value of n and only copy it to the end of the string

code reference

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        // write code here
        if(strs.size()==0)return "";//空串返回空值
        for(int i=0;i<strs[0].size();i++){//比较第一个字符串的每一个字符
            for(int j=1;j<strs.size();j++){//纵向比较字符串
                if(strs[0][i]!=strs[j][i]||i==strs[j].size()){//比较后面的每一个字符串
                    return strs[0].substr(0,i);//若字符不相同或者长度为最小则返回最长公共前缀
                }
            }
        }
        return strs[0];
    }
};

Source of topic:

Longest Common Prefix_Niuke Questions_Niuke.com 

Guess you like

Origin blog.csdn.net/m0_52124992/article/details/131796678