📌  相关文章
📜  查找字符在前一个位置出现的次数

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

查找字符在前一个位置出现的次数

给定一个长度为N的字符串S和一个整数P (1≤P≤N) 表示字符串中字符的位置。任务是找到出现在位置 P 到 P-1 索引处的字符的出现次数。

例子:

朴素的方法:一种朴素的方法是遍历字符串直到 Position-1 搜索相似的字符。每当出现类似字符时,将计数器变量加一。

下面是上述方法的实现:

C++
// CPP program to find the number of occurrences
// of a character at position P upto p-1
#include 
using namespace std;
 
// Function to find the number of occurrences
// of a character at position P upto p-1
int Occurrence(string s, int position)
{
    int count = 0;
    for (int i = 0; i < position - 1; i++)
        if (s[i] == s[position - 1])
            count++;
 
    // Return the required count
    return count;
}
 
// Driver code
int main()
{
    string s = "ababababab";
 
    int p = 9;
 
    // Function call
    cout << Occurrence(s, p);
 
    return 0;
}


Java
// Java program to find the number of occurrences
// of a character at position P upto p-1
class GFG
{
 
// Function to find the number of occurrences
// of a character at position P upto p-1
static int Occurrence(String s, int position)
{
    int count = 0;
    for (int i = 0; i < position - 1; i++)
        if (s.charAt(i) == s.charAt(position - 1))
            count++;
 
    // Return the required count
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "ababababab";
 
    int p = 9;
 
    // Function call
    System.out.println(Occurrence(s, p));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the
# number of occurrences of
# a character at position P upto p-1
 
# Function to find the number of occurrences
# of a character at position P upto p-1
def Occurrence(s, position):
    count = 0
    for i in range(position - 1):
        if (s[i] == s[position - 1]):
            count += 1
 
    # Return the required count
    return count
 
# Driver code
s = "ababababab";
 
p = 9
 
# Function call
print(Occurrence(s, p))
 
# This code is contributed by Mohit Kumar


C#
// C# program to find the number of occurrences
// of a character at position P upto p-1
using System;
     
class GFG
{
 
// Function to find the number of occurrences
// of a character at position P upto p-1
static int Occurrence(String s, int position)
{
    int count = 0;
    for (int i = 0; i < position - 1; i++)
        if (s[i] == s[position - 1])
            count++;
 
    // Return the required count
    return count;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "ababababab";
 
    int p = 9;
 
    // Function call
    Console.WriteLine(Occurrence(s, p));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


C++
// CPP program to find the number of occurrences
// of a character at position P upto p-1
#include 
using namespace std;
 
// Function to find the number of occurrences
// of a character at position P upto p-1
int countOccurrence(string s, int position)
{
    int alpha[26] = { 0 }, b[s.size()] = { 0 };
 
    // Iterate over the string
    for (int i = 0; i < s.size(); i++) {
        // Store the Occurrence of same character
        b[i] = alpha[(int)s[i] - 97];
 
        // Increase its frequency
        alpha[(int)s[i] - 97]++;
    }
 
    // Return the required count
    return b[position - 1];
}
 
// Driver code
int main()
{
    string s = "ababababab";
 
    int p = 9;
 
    // Function call
    cout << countOccurrence(s, p);
 
    return 0;
}


Java
// Java program to find the number of occurrences
// of a character at position P upto p-1
import java.util.*;
 
class GFG
{
 
// Function to find the number of occurrences
// of a character at position P upto p-1
static int countOccurrence(String s, int position)
{
    int []alpha = new int[26];
    int []b = new int[s.length()];
 
    // Iterate over the string
    for (int i = 0; i < s.length(); i++)
    {
        // Store the Occurrence of same character
        b[i] = alpha[(int)s.charAt(i) - 97];
 
        // Increase its frequency
        alpha[(int)s.charAt(i) - 97]++;
    }
 
    // Return the required count
    return b[position - 1];
}
 
// Driver code
public static void main(String[] args)
{
    String s = "ababababab";
 
    int p = 9;
 
    // Function call
    System.out.println(countOccurrence(s, p));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the number of occurrences
# of a character at position P upto p-1
 
# Function to find the number of occurrences
# of a character at position P upto p-1
def countOccurrence(s, position):
    alpha = [0] * 26
    b = [0] * len(s)
     
    # Iterate over the string
    for i in range(0, len(s)):
         
        # Store the Occurrence of same character
        b[i] = alpha[ord(s[i]) - 97]
 
        # Increase its frequency
        alpha[ord(s[i]) - 97] = alpha[ord(s[i]) - 97] + 1
 
    # Return the required count
    return b[position - 1]
 
# Driver code
s = "ababababab"
 
p = 9
 
# Function call
print(countOccurrence(s, p))
 
# This code is contributed by Sanjit_Prasad


C#
// C# program to find the number of occurrences
// of a character at position P upto p-1
using System;
     
class GFG
{
 
// Function to find the number of occurrences
// of a character at position P upto p-1
static int countOccurrence(String s, int position)
{
    int []alpha = new int[26];
    int []b = new int[s.Length];
 
    // Iterate over the string
    for (int i = 0; i < s.Length; i++)
    {
        // Store the Occurrence of same character
        b[i] = alpha[(int)s[i] - 97];
 
        // Increase its frequency
        alpha[(int)s[i] - 97]++;
    }
 
    // Return the required count
    return b[position - 1];
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "ababababab";
 
    int p = 9;
 
    // Function call
    Console.WriteLine(countOccurrence(s, p));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
4

时间复杂度:每个查询的O(N)

有效方法:如果有多个这样的查询,并且我们为每个查询提供一个唯一索引 P,那么一种有效的方法是使用频率数组来存储字符串每次迭代中的字符数。

以下是上述方法的实现:

C++

// CPP program to find the number of occurrences
// of a character at position P upto p-1
#include 
using namespace std;
 
// Function to find the number of occurrences
// of a character at position P upto p-1
int countOccurrence(string s, int position)
{
    int alpha[26] = { 0 }, b[s.size()] = { 0 };
 
    // Iterate over the string
    for (int i = 0; i < s.size(); i++) {
        // Store the Occurrence of same character
        b[i] = alpha[(int)s[i] - 97];
 
        // Increase its frequency
        alpha[(int)s[i] - 97]++;
    }
 
    // Return the required count
    return b[position - 1];
}
 
// Driver code
int main()
{
    string s = "ababababab";
 
    int p = 9;
 
    // Function call
    cout << countOccurrence(s, p);
 
    return 0;
}

Java

// Java program to find the number of occurrences
// of a character at position P upto p-1
import java.util.*;
 
class GFG
{
 
// Function to find the number of occurrences
// of a character at position P upto p-1
static int countOccurrence(String s, int position)
{
    int []alpha = new int[26];
    int []b = new int[s.length()];
 
    // Iterate over the string
    for (int i = 0; i < s.length(); i++)
    {
        // Store the Occurrence of same character
        b[i] = alpha[(int)s.charAt(i) - 97];
 
        // Increase its frequency
        alpha[(int)s.charAt(i) - 97]++;
    }
 
    // Return the required count
    return b[position - 1];
}
 
// Driver code
public static void main(String[] args)
{
    String s = "ababababab";
 
    int p = 9;
 
    // Function call
    System.out.println(countOccurrence(s, p));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to find the number of occurrences
# of a character at position P upto p-1
 
# Function to find the number of occurrences
# of a character at position P upto p-1
def countOccurrence(s, position):
    alpha = [0] * 26
    b = [0] * len(s)
     
    # Iterate over the string
    for i in range(0, len(s)):
         
        # Store the Occurrence of same character
        b[i] = alpha[ord(s[i]) - 97]
 
        # Increase its frequency
        alpha[ord(s[i]) - 97] = alpha[ord(s[i]) - 97] + 1
 
    # Return the required count
    return b[position - 1]
 
# Driver code
s = "ababababab"
 
p = 9
 
# Function call
print(countOccurrence(s, p))
 
# This code is contributed by Sanjit_Prasad

C#

// C# program to find the number of occurrences
// of a character at position P upto p-1
using System;
     
class GFG
{
 
// Function to find the number of occurrences
// of a character at position P upto p-1
static int countOccurrence(String s, int position)
{
    int []alpha = new int[26];
    int []b = new int[s.Length];
 
    // Iterate over the string
    for (int i = 0; i < s.Length; i++)
    {
        // Store the Occurrence of same character
        b[i] = alpha[(int)s[i] - 97];
 
        // Increase its frequency
        alpha[(int)s[i] - 97]++;
    }
 
    // Return the required count
    return b[position - 1];
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "ababababab";
 
    int p = 9;
 
    // Function call
    Console.WriteLine(countOccurrence(s, p));
}
}
 
// This code is contributed by PrinciRaj1992

Javascript


输出:
4