`{"id":"8666f849-60b7-47bc-b7a2-a57e0ee40a17","name":"Group Shifted String","description":"1. You are given an array of strings.\r\n2. You have to group the given strings in such a way that all strings in a group are shifted versions of each other. \r\n3. Two strings s1 and s2 are shifted if -\r\n -> Length of both the strings is the same.\r\n -> The difference between ASCII values of every character of s1 and s2 is constant.\r\n\r\nNote -> Every string consists of lower-case English letters only.","inputFormat":"A number N\r\nstr1\r\nstr2.. N space-separated strings","outputFormat":"Every line of output contains space-separated strings which represents a group of shifted strings.","constraints":"1 &lt;= N &lt;= 10^4\r\n1 &lt;= length of a string &lt;= 100","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass solution{\n public:\n vector<vector<string>> groupStrings(vector<string>& strings) {\n // write your code here\n }\n};\n\nint main(){\n int n;\n cin >> n;\n vector<string> string_list(n);\n for(int i = 0; i < n; i++){\n cin >> string_list[i];\n }\n \n solution ob;\n vector<vector<string>> res = ob.groupStrings(string_list);\n \n for(int i = 0; i < res.size(); i++){\n sort(res[i].begin(),res[i].end());\n }\n \n sort(res.begin(), res.end(),[&](vector<string> a,vector<string> b){\n if(a.size()==b.size()){\n int x=a.compare(b);\n \n return x < 0;\n }\n \n return a.size() > b.size();\n });\n \n for(int i = 0; i < res.size(); i++){\n for(int j = 0; j < res[i].size(); j++){\n cout << res[i][j] << \" \";\n }\n cout << \"\\n\";\n }\n \n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static ArrayList<ArrayList<String>> groupShiftedStrings(String[] strs) {\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tint N = sc.nextInt();\r\n\t\tString[] arr = new String[N];\r\n\t\tfor (int i = 0; i < N; i++) {\r\n\t\t\tarr[i] = sc.next();\r\n\t\t}\r\n\t\tArrayList<ArrayList<String>> shiftedGroup = groupShiftedStrings(arr);\r\n\t\tfor (ArrayList<String> lst : shiftedGroup) {\r\n\t\t\tCollections.sort(lst);\r\n\t\t}\r\n\t\tshiftedGroup.sort(new ListComparator());\r\n\t\tdisplay(shiftedGroup);\r\n\t}\r\n\r\n\t// it is used to make the result unique\r\n\tstatic class ListComparator implements Comparator<List<String>> {\r\n\t\t@Override\r\n\t\tpublic int compare(List<String> l1, List<String> l2) {\r\n\t\t\tif (l1.size() != l2.size()) {\r\n\t\t\t\treturn l2.size() - l1.size();\r\n\t\t\t}\r\n\r\n\t\t\tString l1str = l1.get(0);\r\n\t\t\tString l2str = l2.get(0);\r\n\t\t\treturn l1str.compareTo(l2str);\r\n\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void display(ArrayList<ArrayList<String>> list) {\r\n\t\tfor (int i = 0; i < list.size(); i++) {\r\n\t\t\tArrayList<String> currList = list.get(i);\r\n\t\t\tfor (int j = 0; j < currList.size(); j++) {\r\n\t\t\t\tSystem.out.print(currList.get(j) + \" \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"9\r\nacd dfg wyz yab mop bdfh a x moqs","sampleOutput":"acd dfg mop wyz yab \r\na x \r\nbdfh moqs \r\n","questionVideo":"https://www.youtube.com/embed/uEXJSRLqoKY?end=203","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":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"db7409a1-f1eb-41c2-9bb0-e886ff22581c","name":"Group Shifted String","slug":"group-shifted-string","type":1}],"next":{"id":"87edbf80-aa67-4a81-8afc-45cf5b1054bf","name":"Group Shifted String MCQ","type":0,"slug":"group-shifted-string-mcq"},"prev":{"id":"cdb530b8-1fc1-4b04-a287-d682c2efbe0f","name":"Group Anagrams","type":3,"slug":"group-anagrams"}}}`

# Group Shifted String

1. You are given an array of strings. 2. You have to group the given strings in such a way that all strings in a group are shifted versions of each other. 3. Two strings s1 and s2 are shifted if - -> Length of both the strings is the same. -> The difference between ASCII values of every character of s1 and s2 is constant. Note -> Every string consists of lower-case English letters only.

`{"id":"8666f849-60b7-47bc-b7a2-a57e0ee40a17","name":"Group Shifted String","description":"1. You are given an array of strings.\r\n2. You have to group the given strings in such a way that all strings in a group are shifted versions of each other. \r\n3. Two strings s1 and s2 are shifted if -\r\n -> Length of both the strings is the same.\r\n -> The difference between ASCII values of every character of s1 and s2 is constant.\r\n\r\nNote -> Every string consists of lower-case English letters only.","inputFormat":"A number N\r\nstr1\r\nstr2.. N space-separated strings","outputFormat":"Every line of output contains space-separated strings which represents a group of shifted strings.","constraints":"1 &lt;= N &lt;= 10^4\r\n1 &lt;= length of a string &lt;= 100","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass solution{\n public:\n vector<vector<string>> groupStrings(vector<string>& strings) {\n // write your code here\n }\n};\n\nint main(){\n int n;\n cin >> n;\n vector<string> string_list(n);\n for(int i = 0; i < n; i++){\n cin >> string_list[i];\n }\n \n solution ob;\n vector<vector<string>> res = ob.groupStrings(string_list);\n \n for(int i = 0; i < res.size(); i++){\n sort(res[i].begin(),res[i].end());\n }\n \n sort(res.begin(), res.end(),[&](vector<string> a,vector<string> b){\n if(a.size()==b.size()){\n int x=a.compare(b);\n \n return x < 0;\n }\n \n return a.size() > b.size();\n });\n \n for(int i = 0; i < res.size(); i++){\n for(int j = 0; j < res[i].size(); j++){\n cout << res[i][j] << \" \";\n }\n cout << \"\\n\";\n }\n \n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static ArrayList<ArrayList<String>> groupShiftedStrings(String[] strs) {\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tint N = sc.nextInt();\r\n\t\tString[] arr = new String[N];\r\n\t\tfor (int i = 0; i < N; i++) {\r\n\t\t\tarr[i] = sc.next();\r\n\t\t}\r\n\t\tArrayList<ArrayList<String>> shiftedGroup = groupShiftedStrings(arr);\r\n\t\tfor (ArrayList<String> lst : shiftedGroup) {\r\n\t\t\tCollections.sort(lst);\r\n\t\t}\r\n\t\tshiftedGroup.sort(new ListComparator());\r\n\t\tdisplay(shiftedGroup);\r\n\t}\r\n\r\n\t// it is used to make the result unique\r\n\tstatic class ListComparator implements Comparator<List<String>> {\r\n\t\t@Override\r\n\t\tpublic int compare(List<String> l1, List<String> l2) {\r\n\t\t\tif (l1.size() != l2.size()) {\r\n\t\t\t\treturn l2.size() - l1.size();\r\n\t\t\t}\r\n\r\n\t\t\tString l1str = l1.get(0);\r\n\t\t\tString l2str = l2.get(0);\r\n\t\t\treturn l1str.compareTo(l2str);\r\n\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void display(ArrayList<ArrayList<String>> list) {\r\n\t\tfor (int i = 0; i < list.size(); i++) {\r\n\t\t\tArrayList<String> currList = list.get(i);\r\n\t\t\tfor (int j = 0; j < currList.size(); j++) {\r\n\t\t\t\tSystem.out.print(currList.get(j) + \" \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"9\r\nacd dfg wyz yab mop bdfh a x moqs","sampleOutput":"acd dfg mop wyz yab \r\na x \r\nbdfh moqs \r\n","questionVideo":"https://www.youtube.com/embed/uEXJSRLqoKY?end=203","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":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"db7409a1-f1eb-41c2-9bb0-e886ff22581c","name":"Group Shifted String","slug":"group-shifted-string","type":1}],"next":{"id":"87edbf80-aa67-4a81-8afc-45cf5b1054bf","name":"Group Shifted String MCQ","type":0,"slug":"group-shifted-string-mcq"},"prev":{"id":"cdb530b8-1fc1-4b04-a287-d682c2efbe0f","name":"Group Anagrams","type":3,"slug":"group-anagrams"}}}` Editor

# Group Shifted String

medium

1. You are given an array of strings. 2. You have to group the given strings in such a way that all strings in a group are shifted versions of each other. 3. Two strings s1 and s2 are shifted if - -> Length of both the strings is the same. -> The difference between ASCII values of every character of s1 and s2 is constant. Note -> Every string consists of lower-case English letters only.

## Constraints

1 <= N <= 10^4 1 <= length of a string <= 100

## Format

### Input

A number N str1 str2.. N space-separated strings

### Output

Every line of output contains space-separated strings which represents a group of shifted strings.

## 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;}9 acd dfg wyz yab mop bdfh a x moqs```

### 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;}acd dfg mop wyz yab a x bdfh moqs ```

Question Video

Discussions

Show Discussion

Related Resources 