Last active
March 15, 2023 19:01
-
-
Save sharunkumar/45f4e3c152abd4052c82f9ee48392e3d to your computer and use it in GitHub Desktop.
String Programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java program to check if two strings | |
// areIsomorphic | |
import java.io.*; | |
import java.util.*; | |
class GFG { | |
static boolean areIsomorphic(String str1, String str2) | |
{ | |
HashMap<Character, Character> charCount | |
= new HashMap(); | |
char c = 'a'; | |
for (int i = 0; i < str1.length(); i++) { | |
if (charCount.containsKey(str1.charAt(i))) { | |
c = charCount.get(str1.charAt(i)); | |
if (c != str2.charAt(i)) | |
return false; | |
} | |
else if (!charCount.containsValue( | |
str2.charAt(i))) { | |
charCount.put(str1.charAt(i), | |
str2.charAt(i)); | |
} | |
else { | |
return false; | |
} | |
} | |
return true; | |
} | |
/* Driver code*/ | |
public static void main(String[] args) | |
{ | |
String str1 = "aac"; | |
String str2 = "xxy"; | |
// Function Call | |
if (str1.length() == str2.length() | |
&& areIsomorphic(str1, str2)) | |
System.out.println("True"); | |
else | |
System.out.println("False"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int lengthOfLongestSubstring(String s) { | |
Map<Character, Integer> chars = new HashMap(); | |
int left = 0; | |
int right = 0; | |
int res = 0; | |
// sliding window | |
// |---|-------|---| | |
// left right | |
while(right < s.length()) { | |
char current = s.charAt(right); | |
// keep incrementing count right pointer character | |
chars.put(current, chars.getOrDefault(current, 0) + 1); | |
while(chars.get(current) > 1) { | |
// repeat detected | |
// shorten window until no repeats | |
char back = s.charAt(left); | |
chars.put(back, chars.get(back) -1); | |
left ++; | |
} | |
res = Math.max(right-left+1, res); | |
right++; | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public List<String> generateParenthesis(int n) { | |
List<String> ans = new ArrayList(); | |
if (n == 0) { | |
ans.add(""); | |
} else { | |
for (int c = 0; c < n; ++c) | |
for (String left: generateParenthesis(c)) | |
for (String right: generateParenthesis(n-1-c)) | |
ans.add("(" + left + ")" + right); | |
} | |
return ans; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
static Map<Character, Integer> map = new HashMap<>(); | |
static { | |
map.put('I', 1); | |
map.put('V', 5); | |
map.put('X', 10); | |
map.put('L', 50); | |
map.put('C', 100); | |
map.put('D', 500); | |
map.put('M', 1000); | |
} | |
public int romanToInt(String s) { | |
int i = 0; | |
int sum = 0; | |
while(i<s.length() - 1) { | |
if(map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) { | |
sum += map.get(s.charAt(i+1)) - map.get(s.charAt(i)); | |
i += 2; | |
} | |
else { | |
sum += map.get(s.charAt(i)); | |
i += 1; | |
} | |
} | |
if(i == s.length() - 1) { | |
sum += map.get(s.charAt(i)); | |
} | |
return sum; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Java code to implement the approach | |
import java.io.*; | |
class GFG { | |
// Function to return shortest distance | |
static int shortestDistance(String[] s, | |
String word1, String word2) | |
{ | |
int d1 = -1, d2 = -1; | |
int ans = Integer.MAX_VALUE; | |
// Traverse the string | |
for (int i = 0; i < s.length; i++) { | |
if (s[i] == word1) | |
d1 = i; | |
if (s[i] == word2) | |
d2 = i; | |
if (d1 != -1 && d2 != -1) | |
ans = Math.min(ans, Math.abs(d1 - d2)); | |
} | |
// Return the answer | |
return ans; | |
} | |
// Driver Code | |
public static void main (String[] args) { | |
String[] S | |
= { "the", "quick", "brown", "fox", "quick" }; | |
String word1 = "the", word2 = "fox"; | |
// Function Call | |
System.out.println(shortestDistance(S, word1, word2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment