├── PalindromeString.md ├── README.md ├── ReverseWords.md └── RomanToInteger.md /PalindromeString.md: -------------------------------------------------------------------------------- 1 | ```java 2 | class Solution { 3 | int isPalindrome(String S) { 4 | // using reverse method 5 | StringBuilder stb = new StringBuilder(S); 6 | String st = stb.reverse().toString(); 7 | if(st.equals(S)){ 8 | return 1; 9 | } 10 | return 0; 11 | } 12 | } 13 | ``` 14 | 15 | ```java 16 | class Solution { 17 | int isPalindrome(String S) { 18 | //using 2 pointer approach 19 | int i=0,j=S.length()-1; 20 | while(i<=j){ 21 | if(S.charAt(i++)==S.charAt(j--)){ 22 | continue; 23 | }else{ 24 | return 0; 25 | } 26 | } 27 | return 1; 28 | } 29 | }; 30 | ``` 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StringCodingQuestions 🚀 2 | Mostly Asked String Interview Coding Questions | String Programs ( Problems ) for Practice 👩🏻‍💻 3 | 4 | ## Foundation Questions 🚀 5 | | String Questions | Practice Link | Code | Video Solutions | HINT 6 | | ------------- | :-------------: | :-------------: | :---------: |:---------: | 7 | Check Palindrome String(reverse string is same) | [Solve](https://practice.geeksforgeeks.org/problems/palindrome-string0817/1) | [Code](https://github.com/RecursiveSharma/StringCodingQuestions/blob/main/PalindromeString.md) | | 2 pointer 8 | Anagram Strings(rearranging letters) | [Solve](https://leetcode.com/problems/valid-anagram/) | 9 | Valid Parenthesis [({ })] | [Solve](https://leetcode.com/problems/valid-parentheses/) | [Code]() | [Video](https://www.youtube.com/watch?v=rvFnUMlwUYo) | Stack 10 | Roman Number to Integer | [Solve](https://leetcode.com/problems/roman-to-integer/) | [Code](https://github.com/RecursiveSharma/StringCodingQuestions/blob/main/RomanToInteger.md) | [Video](https://youtu.be/Cl-FZVo69gk) | Use Map 11 | Check for subsequence | [Solve](https://leetcode.com/problems/is-subsequence/) | 12 | Implement strStr | [Solve](https://leetcode.com/problems/implement-strstr/) | 13 | 14 | 15 | ## Medium Questions 🚀 16 | | String Questions | Practice Link | Code | Video Solutions | HINT 17 | | ------------- | :-------------: | :-------------: | :---------: |:---------: | 18 | Reverse Words(each word) in a String | [Solve](https://leetcode.com/problems/reverse-words-in-a-string/) | [Code](https://github.com/RecursiveSharma/StringCodingQuestions/blob/main/ReverseWords.md)| [Video](https://youtu.be/rt67FWYD1mQ) | Word Seperate by spaces 19 | Form a Longest palindrome | [Solve](https://leetcode.com/problems/longest-palindrome/) | 20 | Longest Palindrome in a String | [Solve1](https://practice.geeksforgeeks.org/problems/longest-palindrome-in-a-string3411/1) & [Solve2](https://leetcode.com/problems/longest-palindrome/) 21 | Multiply two strings | [Solve](https://leetcode.com/problems/multiply-strings/) 22 | Longest Common Substring | [Solve](https://practice.geeksforgeeks.org/problems/longest-common-substring1452/1) 23 | Longest Palindromic Substring | [Solve](https://leetcode.com/problems/longest-palindromic-substring/) 24 | Length of the longest substring | [Solve](https://practice.geeksforgeeks.org/problems/length-of-the-longest-substring3036/1) 25 | Longest Prefix Suffix | [Solve1](https://practice.geeksforgeeks.org/problems/longest-prefix-suffix2527/1) & [Solve2](https://leetcode.com/problems/longest-common-prefix/) 26 | Permutations of a given string | [Solve](https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string2041/1) 27 | Excel Sheet | [Solve1]( https://leetcode.com/problems/excel-sheet-column-number/) & [Solve2](https://practice.geeksforgeeks.org/problems/excel-sheet5448/1) 28 | Validate an IP Address | [Solve](https://leetcode.com/problems/validate-ip-address/) & [Solve2](https://practice.geeksforgeeks.org/problems/validate-an-ip-address-1587115621/1/) 29 | Count and Say | [Solve](https://leetcode.com/problems/count-and-say/) 30 | 31 | ## Hard Questions 🚀 32 | | String Questions | Practice Link | Code | Video Solutions | HINT 33 | | ------------- | :-------------: | :-------------: | :---------: |:---------: | 34 | Longest valid Parentheses | [Solve](https://leetcode.com/problems/longest-valid-parentheses/) 35 | Edit Distance | [Solve](https://leetcode.com/problems/edit-distance/) 36 | Alien Dictionary | [Solve](https://practice.geeksforgeeks.org/problems/alien-dictionary/1) 37 | -------------------------------------------------------------------------------- /ReverseWords.md: -------------------------------------------------------------------------------- 1 | ### [Back2Home](https://github.com/RecursiveSharma/StringCodingQuestions) | [Go2Video](https://youtu.be/rt67FWYD1mQ) 2 | ```java 3 | 4 | //sol1 = Best Time Soln with String Builder = O(n) 5 | class Solution { 6 | public String reverseWords(String s) { 7 | 8 | if(s.length()==0 || s==null) return ""; 9 | 10 | StringBuilder sb=new StringBuilder(); 11 | String[] str_arr=s.split(" "); 12 | int n=str_arr.length-1; 13 | for(int i=n;i>=0;i--){ 14 | if(!str_arr[i].equals("")){ 15 | sb.append(str_arr[i]).append(" "); 16 | } 17 | } 18 | return sb.length()==0?"":sb.substring(0,sb.length()-1); 19 | } 20 | } 21 | 22 | ``` 23 | 24 | ```java 25 | 26 | // Solution O(n) time complexity 27 | class Solution { 28 | public String reverseWords(String s) { 29 | 30 | if(s.length()==0 || s==null) return ""; 31 | 32 | s = s.replaceAll("\\s++", " ").trim(); 33 | String[] str = s.split(" "); 34 | 35 | String result = ""; 36 | int last = str.length - 1; 37 | for (int i = last; i > 0; i--) { 38 | result += str[i] + " "; 39 | } 40 | 41 | return result += str[0]; 42 | } 43 | } 44 | ``` 45 | 46 | ```java 47 | 48 | //sol3 = simple Naive Soln = O(n^2) time complxity 49 | 50 | if(s.length()==0 || s==null) return ""; 51 | 52 | int i = 0; 53 | int n = s.length(); 54 | String res = ""; 55 | while (i < n) { 56 | while (i < n && s.charAt(i) == ' ') { 57 | i++; 58 | } 59 | if (i >= n) { 60 | break; 61 | } 62 | int j = i + 1; 63 | while (j < n && s.charAt(j) != ' ') { 64 | j++; 65 | } 66 | String w = s.substring(i,j); 67 | 68 | if (res.length() == 0) { 69 | res = w; 70 | } else { 71 | res = w + " " + res; 72 | } 73 | i = j+1; 74 | 75 | } 76 | 77 | return res; 78 | 79 | -------------------------------------------------------------------------------- /RomanToInteger.md: -------------------------------------------------------------------------------- 1 | ### [Back2Home](https://github.com/RecursiveSharma/StringCodingQuestions) | [Go2Video](https://youtu.be/Cl-FZVo69gk) 2 | ```java 3 | 4 | //sol1 = Best Time Soln with map = O(n) 5 | class Solution { 6 | public int romanToInt(String s) { 7 | if(s==null || s.length()==0) return 0; 8 | 9 | Map map= new HashMap(); 10 | map.put('I',1); 11 | map.put('V',5); 12 | map.put('X',10); 13 | map.put('L',50); 14 | map.put('C',100); 15 | map.put('D',500); 16 | map.put('M',1000); 17 | 18 | int result=0; 19 | result=map.get(s.charAt(s.length()-1)); 20 | 21 | for(int i=s.length()-2;i>=0;i--){ 22 | if(map.get(s.charAt(i))= 0; i--) { 42 | if (result == 0) { 43 | result += this.convert(array[i]); 44 | continue; 45 | } 46 | 47 | if (this.convert(array[i]) == this.convert(array[i + 1])) { 48 | result += this.convert(array[i]); 49 | continue; 50 | } 51 | 52 | 53 | if (result > this.convert(array[i])) { 54 | result -= this.convert(array[i]); 55 | continue; 56 | } 57 | 58 | if (result <= this.convert(array[i])) { 59 | result += this.convert(array[i]); 60 | continue; 61 | } 62 | } 63 | 64 | return result; 65 | } 66 | 67 | 68 | 69 | private int convert(char c) { 70 | switch (c) { 71 | case 'I': 72 | return 1; 73 | case 'V': 74 | return 5; 75 | case 'X': 76 | return 10; 77 | case 'L': 78 | return 50; 79 | case 'C': 80 | return 100; 81 | case 'D': 82 | return 500; 83 | case 'M': 84 | return 1000; 85 | 86 | } 87 | return 0; 88 | } 89 | } 90 | 91 | ``` 92 | 93 | ```java 94 | 95 | //sol3 = simple Naive Soln = O(n) time complxity 96 | 97 | 98 | char[] sArr=s.toCharArray(); 99 | int length=s.length(); 100 | int decimalNum; 101 | 102 | for(int i=length-1;i>-1;i--){ 103 | 104 | if( i-1 >-1 && sArr[i]=='V' && sArr[i-1]=='I' ) 105 | { 106 | decimalNum+=4; 107 | i--; 108 | continue; 109 | } 110 | else if( i-1 >-1 && sArr[i]=='X' && sArr[i-1]=='I' ) 111 | { 112 | decimalNum+=9; 113 | i--;continue; 114 | } 115 | else if( i-1 >-1 && sArr[i]=='L' && sArr[i-1]=='X' ) 116 | { 117 | decimalNum+=40; 118 | i--;continue; 119 | } 120 | else if( i-1 >-1 && sArr[i]=='C' && sArr[i-1]=='X' ) 121 | { 122 | decimalNum+=90; 123 | i--;continue; 124 | } 125 | else if( i-1 >-1 && sArr[i]=='D' && sArr[i-1]=='C' ) 126 | { 127 | decimalNum+=400; 128 | i--;continue; 129 | } 130 | else if( i-1 >-1 && sArr[i]=='M' && sArr[i-1]=='C' ) 131 | { 132 | decimalNum+=900; 133 | i--;continue; 134 | } 135 | 136 | 137 | if(i>-1){ 138 | 139 | if(sArr[i]=='I'){ 140 | decimalNum+=1; 141 | }else if(sArr[i]=='V'){ 142 | decimalNum+=5; 143 | }else if(sArr[i]=='X'){ 144 | decimalNum+=10; 145 | }else if(sArr[i]=='L'){ 146 | decimalNum+=50; 147 | }else if(sArr[i]=='C'){ 148 | decimalNum+=100; 149 | }else if(sArr[i]=='D'){ 150 | decimalNum+=500; 151 | } 152 | else{ 153 | decimalNum+=1000; 154 | } 155 | 156 | 157 | } 158 | 159 | 160 | } 161 | return decimalNum; --------------------------------------------------------------------------------