📌  相关文章
📜  检查一个字符串中是否可以多次访问一个单元格

📅  最后修改于: 2021-05-31 19:14:59             🧑  作者: Mango

给定一个字符串,其中包含“。”或任何数字。一种 ‘。’在字符串表示单元格为空,如果有数字x在任何一个单元中,这意味着一个人可以移动x在字符串向右或向左移动。

任务是检查字符串的任何单元格是否可以被多次访问。如果是这样,请打印“是”,否则打印“否”。

例子

Input : str = ".2...2.."
Output: YES
The fourth cell can be visited twice. One way to reach 
the fourth cell is from 2nd cell by moving 2 steps to right
and another way to reach fourth cell is by moving 2 steps 
left from cell 6.

Input : str = ".2...1"
Output: NO
None of the cells in the given string 
can be visited more than once.

这个想法是采用一个数组visit []来跟踪可访问字符串的第i个单元的次数。现在遍历字符串并检查当前字符是否为’。’。或一个数字x 。如果当前字符是“。”然后如果为数字则不执行任何操作,然后将[ix,i + x]范围内的访问数组的访问计数增加1。

最后,遍历visited []数组,并检查是否有多个单元格被访问了一次以上。

下面是上述方法的实现:

C++
// C++ program to check if any cell of the
// string can be visited more than once
  
#include 
using namespace std;
  
// Function to check if any cell can be
// visited more than once
bool checkIfOverlap(string str)
{
    int len = str.length();
  
    // Array to mark cells
    int visited[len + 1] = { 0 };
  
    // Traverse the string
    for (int i = 0; i < len; i++) {
        if (str[i] == '.')
            continue;
  
        // Increase the visit count of the left and right
        // cells within the array which can be visited
        for (int j = max(0, i - str[i]); j <= min(len, i + str[i]); j++)
            visited[j]++;
    }
  
    for (int i = 0; i < len; i++) {
        // If any cell can be visited more than once
        // Return True
        if (visited[i] > 1) {
            return true;
        }
    }
  
    return false;
}
  
// Driver code
int main()
{
    string str = ".2..2.";
  
    if (checkIfOverlap(str))
        cout << "YES";
    else
        cout << "NO";
  
    return 0;
}


Java
// Java program to check if any cell of the
// string can be visited more than once
  
import java.io.*;
  
class GFG {
  
// Function to check if any cell can be
// visited more than once
static boolean checkIfOverlap(String str)
{
    int len = str.length();
  
    // Array to mark cells
    int []visited =  new int[len +1];
  
    // Traverse the string
    for (int i = 0; i < len; i++) {
        if (str.charAt(i)== '.')
            continue;
  
        // Increase the visit count of the left and right
        // cells within the array which can be visited
        for (int j = Math.max(0, i - str.charAt(i)); j <= Math.min(len, i + str.charAt(i)); j++)
            visited[j]++;
    }
  
    for (int i = 0; i < len; i++) {
        // If any cell can be visited more than once
        // Return True
        if (visited[i] > 1) {
            return true;
        }
    }
  
    return false;
}
  
// Driver code
  
    public static void main (String[] args) {
        String str = ".2..2.";
  
    if (checkIfOverlap(str))
        System.out.println("YES");
    else
        System.out.print("NO");
    }
}
  
// This code is contributed by inder_verma..


Python 3
# Python3 program to check if 
# any cell of the string can 
# be visited more than once
  
# Function to check if any cell 
# can be visited more than once 
def checkIfOverlap(str) :
  
    length = len(str)
  
    # Array to mark cells
    visited = [0] * (length + 1)
  
    # Traverse the string 
    for i in range(length) :
        if str[i] == "." :
            continue
  
        # Increase the visit count 
        # of the left and right cells
        # within the array which can 
        # be visited 
        for j in range(max(0, i - ord(str[i]), 
                       min(length, i + 
                       ord(str[i])) + 1)) :
            visited[j] += 1
  
    # If any cell can be visited 
    # more than once, Return True 
    for i in range(length) :
  
        if visited[i] > 1 :
            return True
  
    return False
  
# Driver code     
if __name__ == "__main__" :
  
    str = ".2..2."
  
    if checkIfOverlap(str) :
        print("YES")
                          
    else :
        print("NO")
                      
# This code is contributed 
# by ANKITRAI1


C#
// C# program to check if any 
// cell of the string can be
// visited more than once
using System;
  
class GFG 
{
  
// Function to check if any cell 
// can be visited more than once
static bool checkIfOverlap(String str)
{
    int len = str.Length;
  
    // Array to mark cells
    int[] visited = new int[len + 1];
  
    // Traverse the string
    for (int i = 0; i < len; i++) 
    {
        if (str[i]== '.')
            continue;
  
        // Increase the visit count of
        // the left and right cells
        // within the array which can be visited
        for (int j = Math.Max(0, i - str[i]); 
                 j <= Math.Min(len, i + str[i]); j++)
            visited[j]++;
    }
  
    for (int i = 0; i < len; i++) 
    {
        // If any cell can be visited 
        // more than once, Return True
        if (visited[i] > 1)
        {
            return true;
        }
    }
  
    return false;
}
  
// Driver code
public static void Main ()
{
    String str = ".2..2.";
  
    if (checkIfOverlap(str))
        Console.Write("YES");
    else
        Console.Write("NO");
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)


PHP
 1) 
        {
            return true;
        }
    }
  
    return false;
}
  
// Driver code
$str = ".2..2.";
  
if (checkIfOverlap($str))
    echo "YES";
else
    echo "NO";
  
// This code is contributed
// by ChitraNayal
?>


输出:
YES

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。