Find A Sub-Word

Sort by

recency

|

156 Discussions

|

  • + 0 comments

    Java 15:

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n= Integer.parseInt(scanner.nextLine());
            String array[]=new String[n];
            for (int i=0;i<n;i++)   array[i]=scanner.nextLine();
            int q = Integer.parseInt(scanner.nextLine());
            for (int i=0;i<q;i++)
            {   String s = scanner.nextLine();
                String regex = String.format("\\w%s\\w", s);
                Pattern pattern = Pattern.compile(regex);  
                int count=0;
                for(int j=0;j<n;j++)
                {   Matcher matcher = pattern.matcher(array[j]);
                    while(matcher.find())   count++;
                }
                System.out.println(count);
            }
        }
    }
    
  • + 0 comments

    Javascript:

    const lines = input.split(/^\d+\s(?=.*$)/gm);
    const words = lines[2].split("\n");
    words.forEach((word) => {
      const regex = new RegExp(`(?<!\\b)${word}(?!\\b)`, 'g');
      const sum = lines[1].match(regex)?.length || 0;
      console.log(sum);
    });
    
  • + 0 comments

    java solution

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = Integer.parseInt(scanner.nextLine());
            StringBuilder sentenceBuilder = new StringBuilder();
            for (int i = 0; i < n; i++) {
                sentenceBuilder.append(scanner.nextLine());
                sentenceBuilder.append("\n");
            }
            String sentence = sentenceBuilder.toString();
            int q = Integer.parseInt(scanner.nextLine());
            for (int j = 0; j < q; j++) {
                Pattern pattern = Pattern.compile(String.format("\\w%s\\w", scanner.nextLine()));
                Matcher matcher = pattern.matcher(sentence);
                Integer count = 0;
                while (matcher.find()) count++;
                System.out.println(count);
            }
        }
    }
    
  • + 0 comments

    beautiful binary strin

  • + 0 comments

    making anagram