`{"id":"016dc5af-010e-4a24-bff3-98e5752afae5","name":"Kmp Pattern Searching","description":"1. You are given two strings s1 and s2.\r\n2. s1 represents a text and s2 represents a pattern. \r\n3. You have to print the starting index for all occurrences of the pattern in the given text string.","inputFormat":"Two strings s1 and s2 in two different lines.","outputFormat":"Starting index for all occurrences of the pattern in the given text string.","constraints":"1 &lt;= length of the strings &lt;= 10^4","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void solution(String txt, String pat) {\r\n // write your code here\r\n \r\n System.out.println();\r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String s1 = scn.nextLine();\r\n String s2 = scn.nextLine();\r\n\t\tsolution(s1, s2);\r\n\t}\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"abab\r\nab","sampleOutput":"0\r\n2\r\n","questionVideo":"https://www.youtube.com/embed/__Cu92rei1s","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"a2073e25-96a1-4875-b400-f40bbe2edd95","name":"Text Processing For Experts","slug":"text-processing-for-experts-859","type":0},{"id":"3b0de180-8222-4a11-8dca-116ef481da5a","name":"Kmp Pattern Searching","slug":"kmp-pattern-searching","type":1}],"next":{"id":"0f1ba7c1-21bb-41ca-bf5f-6868b1670046","name":"Z Algorithm For Pattern Searching","type":1,"slug":"z-algorithm-for-pattern-searching"},"prev":{"id":"c01c3424-51ff-47f1-95c8-710635fd2aed","name":"Shortest Palindrome","type":1,"slug":"shortest-palindrome"}}}`

# Kmp Pattern Searching

1. You are given two strings s1 and s2. 2. s1 represents a text and s2 represents a pattern. 3. You have to print the starting index for all occurrences of the pattern in the given text string.

`{"id":"016dc5af-010e-4a24-bff3-98e5752afae5","name":"Kmp Pattern Searching","description":"1. You are given two strings s1 and s2.\r\n2. s1 represents a text and s2 represents a pattern. \r\n3. You have to print the starting index for all occurrences of the pattern in the given text string.","inputFormat":"Two strings s1 and s2 in two different lines.","outputFormat":"Starting index for all occurrences of the pattern in the given text string.","constraints":"1 &lt;= length of the strings &lt;= 10^4","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void solution(String txt, String pat) {\r\n // write your code here\r\n \r\n System.out.println();\r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String s1 = scn.nextLine();\r\n String s2 = scn.nextLine();\r\n\t\tsolution(s1, s2);\r\n\t}\r\n\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"abab\r\nab","sampleOutput":"0\r\n2\r\n","questionVideo":"https://www.youtube.com/embed/__Cu92rei1s","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"a2073e25-96a1-4875-b400-f40bbe2edd95","name":"Text Processing For Experts","slug":"text-processing-for-experts-859","type":0},{"id":"3b0de180-8222-4a11-8dca-116ef481da5a","name":"Kmp Pattern Searching","slug":"kmp-pattern-searching","type":1}],"next":{"id":"0f1ba7c1-21bb-41ca-bf5f-6868b1670046","name":"Z Algorithm For Pattern Searching","type":1,"slug":"z-algorithm-for-pattern-searching"},"prev":{"id":"c01c3424-51ff-47f1-95c8-710635fd2aed","name":"Shortest Palindrome","type":1,"slug":"shortest-palindrome"}}}`

Editor

# Kmp Pattern Searching

hard

1. You are given two strings s1 and s2. 2. s1 represents a text and s2 represents a pattern. 3. You have to print the starting index for all occurrences of the pattern in the given text string.

## Constraints

1 <= length of the strings <= 10^4

## Format

### Input

Two strings s1 and s2 in two different lines.

### Output

Starting index for all occurrences of the pattern in the given text string.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}abab ab```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}0 2 ```

Question Video

Discussions

Show Discussion

Related Resources