📌  相关文章
📜  从给定字符串的开头找到第 K 个不同的字符

📅  最后修改于: 2022-05-13 01:57:07.410000             🧑  作者: Mango

从给定字符串的开头找到第 K 个不同的字符

给定一个大小为N的字符串str ,其中包含所有可能的字符,包括整数。从给定字符串的开头打印第 K个不同的字符。如果K大于不同字符的数量,则打印-1

笔记: 大写和小写字母被认为是不同的。

例子:

天真的方法:一个简单的解决方案是使用两个嵌套循环,其中外循环从左到右选择字符,内循环检查所选择的字符是否存在于其他地方并将其设为空字符'\0'。如果第 i 个元素不是 '\0',则增加不同元素的计数。如果不同元素计数变为K ,则返回当前元素。

下面是上述方法的实现。

C++14
// C++ program to implement above approach
#include 
using namespace std;
 
// Function to print the Kth distinct character
int printKDistinct(string& str, int& K)
{
    int distinct = 0, i, j, N = str.length();
 
    if (K <= 0)
        return -1;
 
    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
            if (str[i] == str[j])
                str[j] = '\0';
        }
        if (str[i] != '\0')
            distinct++;
        if (distinct == K)
            return str[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int K = 2;
    int ans = printKDistinct(str, K);
    if (ans == -1)
        cout << -1;
    else
        cout << (char)ans;
    return 0;
}


Javascript


C++14
// C++ program to implement the approach
#include 
using namespace std;
 
// Function to print the Kth distinct character
int printKDistinct(string& str, int& K)
{
    set unique;
    int N = str.length();
 
    for (int i = 0; i < N; i++) {
        unique.insert(str[i]);
        if (unique.size() == K)
            return str[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int K = 2;
    int ans = printKDistinct(str, K);
    if (ans == -1)
        cout << -1;
    else
        cout << (char)ans;
    return 0;
}


Python3
# Python 3 program to implement the approach
 
# Function to print the Kth distinct character
def printKDistinct(st,  K):
 
    unique = set([])
    N = len(st)
 
    for i in range(N):
        unique.add(st[i])
        if (len(unique) == K):
            return st[i]
 
    return -1
 
# Driver code
if __name__ == "__main__":
 
    st = "GeeksForGeeks"
    K = 2
    ans = printKDistinct(st, K)
    if (ans == -1):
        print(-1)
    else:
        print(ans)
 
        # This code is contributed by ukasp.


Javascript


C++14
// C++ program to implement the approach
#include 
using namespace std;
 
// Function to print the Kth distinct character
int printKDistinct(string& str, int& K)
{
    int distinct = 0, N = str.length();
    unordered_map freq;
 
    for (int i = 0; i < N; i++) {
        if (!freq[str[i]])
            distinct++;
        freq[str[i]] = 1;
        if (distinct == K)
            return str[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int K = 2;
    int ans = printKDistinct(str, K);
    if (ans == -1)
        cout << -1;
    else
        cout << (char)ans;
    return 0;
}


Javascript



输出
e

时间复杂度: O(N * N)
辅助空间: O(1)

使用集合:一种有效的方法是使用 设置通过遍历给定字符串来存储字符并在每次迭代时检查其大小。如果它的大小等于K则返回当前字符。否则, K大于字符串中存在的不同字符的数量,因此返回-1

下面是上述方法的实现。

C++14

// C++ program to implement the approach
#include 
using namespace std;
 
// Function to print the Kth distinct character
int printKDistinct(string& str, int& K)
{
    set unique;
    int N = str.length();
 
    for (int i = 0; i < N; i++) {
        unique.insert(str[i]);
        if (unique.size() == K)
            return str[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int K = 2;
    int ans = printKDistinct(str, K);
    if (ans == -1)
        cout << -1;
    else
        cout << (char)ans;
    return 0;
}

Python3

# Python 3 program to implement the approach
 
# Function to print the Kth distinct character
def printKDistinct(st,  K):
 
    unique = set([])
    N = len(st)
 
    for i in range(N):
        unique.add(st[i])
        if (len(unique) == K):
            return st[i]
 
    return -1
 
# Driver code
if __name__ == "__main__":
 
    st = "GeeksForGeeks"
    K = 2
    ans = printKDistinct(st, K)
    if (ans == -1):
        print(-1)
    else:
        print(ans)
 
        # This code is contributed by ukasp.

Javascript



输出
e

时间复杂度: O(N * logN)
辅助空间: O(N)

使用哈希映射: 另一个有效的解决方案是使用Hashing 。请按照以下步骤操作:

  • 创建一个空的哈希表
  • 从左到右遍历输入字符串并检查当前字符是否存在于地图中。
  • 如果地图中不存在,则增加distinct
  • 如果distinct变为K ,则返回当前字符。
  • 如果K大于字符串中存在的不同字符的数量,则返回-1

下面是上述方法的实现。

C++14

// C++ program to implement the approach
#include 
using namespace std;
 
// Function to print the Kth distinct character
int printKDistinct(string& str, int& K)
{
    int distinct = 0, N = str.length();
    unordered_map freq;
 
    for (int i = 0; i < N; i++) {
        if (!freq[str[i]])
            distinct++;
        freq[str[i]] = 1;
        if (distinct == K)
            return str[i];
    }
    return -1;
}
 
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int K = 2;
    int ans = printKDistinct(str, K);
    if (ans == -1)
        cout << -1;
    else
        cout << (char)ans;
    return 0;
}

Javascript



输出
e

时间复杂度: O(N)
辅助空间: O(N)