📜  从字符串查找网站的唯一 ID 和域名

📅  最后修改于: 2021-09-06 06:43:36             🧑  作者: Mango

考虑到大小为N的字符串S组成的独特的网站的唯一ID域名的,任务是找到ID域名给定字符串中如果ID的形式[炭,炭,炭,炭,字符,数字,数字,数字,数字,字符]

例子:

方法:解决给定问题的最简单方法是将字符串拆分为单词并查找拆分的字符串是ID还是Domain 。请按照以下步骤解决问题:

  • 首先拆分由空格分隔的字符串的单词,并将它们存储在字符串向量 say words[] 中
  • 初始化两个空字符串,比如IDDomain来存储结果ID域名
  • 遍历字符串words[]的向量并执行以下步骤:
    • 初始化一个变量,如果当前字符串满足ID的格式,则说flagfalse来存储。
    • 如果当前字符串的长度是10并且如果前5 个字符和最后一个字符是非字母或字符串的任何剩余字符是非数字,则将该标志标记true
    • 如果标志的值为false ,则将当前字符串分配给ID
    • 如果当前字符串的第一个子串,例如[0, 2]范围内的SS是“ www ”,而[SS.length() – 3, SS.length() – 1]范围内的子串是“ com ”,则分配域名,即在[3, SS.length() – 1]范围内的子字符串到Domain
  • 完成以上步骤后,打印IDDomain的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if a character is
// alphabet or not
bool ischar(char x)
{
    if ((x >= 'A' && x <= 'Z')
        || (x >= 'a' && x <= 'z')) {
        return 1;
    }
 
    return 0;
}
 
// Function to check if a character is
// a numeric or not
bool isnum(char x)
{
    if (x >= '0' && x <= '9')
        return 1;
    return 0;
}
 
// Function to find ID and Domain
// name from a given string
void findIdandDomain(string S, int N)
{
    // Stores ID and the domain names
    string ID, Domain;
 
    // Stores the words of string S
    vector words;
 
    // Stores the temporary word
    string curr = "";
 
    // Traverse the string S
    for (int i = 0; i < N; i++) {
 
        // If the current character
        // is space
        if (S[i] == ' ') {
 
            // Push the curr in words
            words.push_back(curr);
 
            // Update the curr
            curr = "";
        }
 
        // Otherwise
        else {
            if (S[i] == '.') {
 
                if (i + 1 == N
                    || (i + 1 < N
                        && S[i + 1] == ' '))
                    continue;
            }
            curr += S[i];
        }
    }
 
    // If curr is not empty
    if (curr.length())
        words.push_back(curr);
 
    for (string ss : words) {
 
        // If length of ss is 10
        if (ss.size() == 10) {
            bool flag = 0;
 
            // Traverse the string ss
            for (int j = 0; j <= 9; j++) {
 
                // If j is in the range
                // [5, 9)
                if (j >= 5 && j < 9) {
 
                    // If current character
                    // is not numeric
                    if (isnum(ss[j]) == 0)
 
                        // Mark flag 1
                        flag = 1;
                }
 
                // Otherwise
                else {
 
                    // If current character
                    // is not alphabet
                    if (ischar(ss[j]) == 0)
 
                        // Mark flag 1
                        flag = 1;
                }
            }
 
            // If flag is false
            if (!flag) {
 
                // Assign ss to ID
                ID = ss;
            }
        }
 
        // If substring formed by the
        // first 3 character is "www"
        // and last 3 character is "moc"
        if (ss.substr(0, 3) == "www"
            && ss.substr(
                   ss.length() - 3, 3)
                   == "com") {
 
            // Update the domain name
            Domain = ss.substr(
                4, ss.size() - 4);
        }
    }
 
    // Print ID and Domain
    cout << "ID = " << ID
         << endl;
    cout << "Domain = " << Domain;
}
 
// Driver Code
int main()
{
    string S = "We thank ABCDE1234F for visiting "
               "us and buying "
               "products item AMZrr@!k. For more "
               "offers, visit "
               "us at www.amazon.com";
    int N = S.length();
    findIdandDomain(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if a character is
// alphabet or not
static boolean ischar(char x)
{
    if ((x >= 'A' && x <= 'Z') ||
        (x >= 'a' && x <= 'z'))
    {
        return true;
    }
    return false;
}
 
// Function to check if a character is
// a numeric or not
static boolean isnum(char x)
{
    if (x >= '0' && x <= '9')
        return true;
         
    return false;
}
 
// Function to find ID and Domain
// name from a given String
static void findIdandDomain(String S, int N)
{
     
    // Stores ID and the domain names
    String ID = "", Domain = "";
 
    // Stores the words of String S
    Vector words = new Vector();
 
    // Stores the temporary word
    String curr = "";
 
    // Traverse the String S
    for(int i = 0; i < N; i++)
    {
         
        // If the current character
        // is space
        if (S.charAt(i) == ' ')
        {
             
            // Push the curr in words
            words.add(curr);
 
            // Update the curr
            curr = "";
        }
 
        // Otherwise
        else
        {
            if (S.charAt(i) == '.')
            {
                 
                if (i + 1 == N || (i + 1 < N &&
                    S.charAt(i + 1) == ' '))
                    continue;
            }
            curr += S.charAt(i);
        }
    }
 
    // If curr is not empty
    if (curr.length() > 0)
        words.add(curr);
 
    for(String ss : words)
    {
         
        // If length of ss is 10
        if (ss.length() == 10)
        {
            boolean flag = false;
 
            // Traverse the String ss
            for(int j = 0; j <= 9; j++)
            {
 
                // If j is in the range
                // [5, 9)
                if (j >= 5 && j < 9)
                {
 
                    // If current character
                    // is not numeric
                    if (isnum(ss.charAt(j)) == false)
 
                        // Mark flag 1
                        flag = true;
                }
 
                // Otherwise
                else
                {
                     
                    // If current character
                    // is not alphabet
                    if (ischar(ss.charAt(j)) == false)
 
                        // Mark flag 1
                        flag = true;
                }
            }
 
            // If flag is false
            if (!flag)
            {
                 
                // Assign ss to ID
                ID = ss;
            }
        }
 
        // If subString formed by the
        // first 3 character is "www"
        // and last 3 character is "moc"
        if (ss.length() > 2 && ss.substring(0, 3).equals("www") &&
            ss.substring(ss.length() - 3).equals("com"))
        {
 
            // Update the domain name
            Domain = ss.substring(4, ss.length());
        }
    }
 
    // Print ID and Domain
    System.out.print("ID = " +  ID + "\n");
    System.out.print("Domain = " +  Domain);
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "We thank ABCDE1234F for visiting " +
               "us and buying products item AMZrr@!k. " +
               "For more offers, visit us at www.amazon.com";
    int N = S.length();
     
    findIdandDomain(S, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to check if a character is
# alphabet or not
def ischar(x):
     
    if ((x >= 'A' and x <= 'Z') or
        (x >= 'a' and x <= 'z')):
        return 1
         
    return 0
 
# Function to check if a character is
# a numeric or not
def isnum(x):
     
    if (x >= '0' and x <= '9'):
        return 1
         
    return 0
 
# Function to find ID and Domain
# name from a given
def findIdandDomain(S, N):
     
    # Stores ID and the domain names
    ID, Domain = "", ""
 
    # Stores the words of  S
    words = []
 
    # Stores the temporary word
    curr = ""
 
    # Traverse the  S
    for i in range(N):
         
        # If the current character
        # is space
        if (S[i] == ' '):
             
            # Push the curr in words
            words.append(curr)
             
            # Update the curr
            curr = ""
             
        # Otherwise
        else:
            if (S[i] == '.'):
                if (i + 1 == N or (i + 1 < N and
                  S[i + 1] == ' ')):
                    continue
                 
            curr += S[i]
 
    # If curr is not empty
    if (len(curr)):
        words.append(curr)
 
    for ss in words:
 
        # If length of ss is 10
        if (len(ss) == 10):
            flag = 0
             
            # Traverse the  ss
            for j in range(10):
 
                # If j is in the range
                # [5, 9)
                if (j >= 5 and j < 9):
                     
                    # If current character
                    # is not numeric
                    if (isnum(ss[j]) == 0):
                         
                        # Mark flag 1
                        flag = 1
                         
                # Otherwise
                else:
                     
                    # If current character
                    # is not alphabet
                    if (ischar(ss[j]) == 0):
                         
                        # Mark flag 1
                        flag = 1
 
            # If flag is false
            if (not flag):
                 
                # Assign ss to ID
                ID = ss
 
        # If sub formed by the
        # first 3 character is "www"
        # and last 3 character is "moc"
        if (ss[0: 3] == "www" and ss[len(ss) - 3: ]== "com"):
             
            # Update the domain name
            Domain = ss[4: len(ss) ]
 
    # Print ID and Domain
    print("ID =", ID)
    print("Domain =", Domain)
 
# Driver Code
if __name__ == '__main__':
     
    S = "We thank ABCDE1234F for visiting us "\
        "and buying products item AMZrr@!k. "\
        "For more offers, visit us at www.amazon.com"
    N = len(S)
     
    findIdandDomain(S, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to check if a character is
// alphabet or not
static bool ischar(char x)
{
    if ((x >= 'A' && x <= 'Z') ||
        (x >= 'a' && x <= 'z'))
    {
        return true;
    }
    return false;
}
 
// Function to check if a character is
// a numeric or not
static bool isnum(char x)
{
    if (x >= '0' && x <= '9')
        return true;
         
    return false;
}
 
// Function to find ID and Domain
// name from a given String
static void findIdandDoMain(String S, int N)
{
     
    // Stores ID and the domain names
    String ID = "", Domain = "";
 
    // Stores the words of String S
    List words = new List();
 
    // Stores the temporary word
    String curr = "";
 
    // Traverse the String S
    for(int i = 0; i < N; i++)
    {
         
        // If the current character
        // is space
        if (S[i] == ' ')
        {
             
            // Push the curr in words
            words.Add(curr);
 
            // Update the curr
            curr = "";
        }
 
        // Otherwise
        else
        {
            if (S[i] == '.')
            {
                 
                if (i + 1 == N || (i + 1 < N &&
                    S[i + 1] == ' '))
                    continue;
            }
            curr += S[i];
        }
    }
 
    // If curr is not empty
    if (curr.Length > 0)
        words.Add(curr);
 
    foreach(String ss in words)
    {
         
        // If length of ss is 10
        if (ss.Length == 10)
        {
            bool flag = false;
 
            // Traverse the String ss
            for(int j = 0; j <= 9; j++)
            {
 
                // If j is in the range
                // [5, 9)
                if (j >= 5 && j < 9)
                {
 
                    // If current character
                    // is not numeric
                    if (isnum(ss[j]) == false)
 
                        // Mark flag 1
                        flag = true;
                }
 
                // Otherwise
                else
                {
                     
                    // If current character
                    // is not alphabet
                    if (ischar(ss[j]) == false)
 
                        // Mark flag 1
                        flag = true;
                }
            }
 
            // If flag is false
            if (!flag)
            {
                 
                // Assign ss to ID
                ID = ss;
            }
        }
 
        // If subString formed by the
        // first 3 character is "www"
        // and last 3 character is "moc"
        if (ss.Length > 2 && ss.Substring(0, 3).Equals("www") &&
            ss.Substring(ss.Length - 3).Equals("com"))
        {
 
            // Update the domain name
            Domain = ss.Substring(4, ss.Length-4);
        }
    }
 
    // Print ID and Domain
    Console.Write("ID = " +  ID + "\n");
    Console.Write("Domain = " +  Domain);
}
 
// Driver Code
public static void Main(String[] args)
{
    String S = "We thank ABCDE1234F for visiting " +
               "us and buying products item AMZrr@!k. " +
               "For more offers, visit us at www.amazon.com";
    int N = S.Length;
     
    findIdandDoMain(S, N);
}
}
 
// This code is contributed by 29AjayKumar


输出:
ID = ABCDE1234F
Domain = amazon.com

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live